Crazy Circuits
You’ve just built a circuit board for your new robot, and now you need to power it. Your robot circuit consists of a number of electrical components that each require a certain amount of current to operate. Every component has a + and a - lead, which are connected on the circuit board at junctions. Current flows through the component from + to - (but note that a component does not "use up" the current: everything that comes in through the + end goes out the - end).
The junctions on the board are labeled 1, ..., N, except for two special junctions labeled + and - where the power supply terminals are connected. The + terminal only connects + leads, and the - terminal only connects - leads. All current that enters a junction from the - leads of connected components exits through connected + leads, but you are able to control how much current flows to each connected + lead at every junction (though methods for doing so are beyond the scope of this problem 1). Moreover, you know you have assembled the circuit in such a way that there are no feedback loops (components chained in a manner that allows current to flow in a loop).
Figure 1: Examples of two valid circuit diagrams.
In (a), all components can be powered along directed paths from the positive terminal to the negative terminal.
In (b), components 4 and 6 cannot be powered, since there is no directed path from junction 4 to the negative terminal.
In the interest of saving power, and also to ensure that your circuit does not overheat, you would like to use as little current as possible to get your robot to work. What is the smallest amount of current that you need to put through the + terminal (which you can imagine all necessarily leaving through the - terminal) so that every component on your robot receives its required supply of current to function?
Hint
1 For those who are electronics-inclined, imagine that you have the ability to adjust the potential on any componentwithout altering its current requirement, or equivalently that there is an accurate variable potentiometer connected in series with each component that you can adjust. Your power supply will have ample potential for the circuit.
Input
The input file will contain multiple test cases. Each test case begins with a single line containing two integers: N (0 <= N <= 50), the number of junctions not including the positive and negative terminals, and M (1 <= M <= 200), the number of components in the circuit diagram. The next M lines each contain a description of some component in the diagram. The i th component description contains three fields: pi, the positive junction to which the component is connected, ni, the negative junction to which the component is connected, and an integer Ii (1 <= Ii <= 100), the minimum amount of current required for component i to function. The junctions pi and ni are specified as either the character '+' indicating the positive terminal, the character '-' indicating the negative terminal, or an integer (between 1 and N) indicating one of the numbered junctions. No two components have the same positive junction and the same negative junction. The end-of-file is denoted by an invalid test case with N = M = 0 and should not be processed.
Output
For each input test case, your program should print out either a single integer indicating the minimum amount of current that must be supplied at the positive terminal in order to ensure that every component is powered, or the message " impossible" if there is no way to direct a sufficient amount of current to each component simultaneously.
Sample Input
6 10 + 1 1 1 2 1 1 3 2 2 4 5 + - 1 4 3 2 3 5 5 4 6 2 5 - 1 6 5 3 4 6 + 1 8 1 2 4 1 3 5 2 4 6 3 - 1 3 4 3 0 0
Sample Output
9 impossible
题解:
上下界有源汇最小流裸题 详解请点击
先化为上下无源汇界可行流, 但是先不加 t --- INF ---> s 这条边,跑一遍ans = maxflow(ss,tt)
然后添加t ---INF---> s 这条边使s,t 流量平衡, 再跑一遍 ans += max(ss,tt).
如果ans == res 即满流的话,就存在可行流,
输出答案 为 最后加的那条边的的流量,即其反向边的容量,es[h[t]^1].c
之所以要先不加t---INF---> s 跑一边maxflow ,是为了尽可能的不用 s--t 之间的路进行增广可行流,这样可以使 t ---INF---- > s 这条边用得尽可能地少,因为t->s只有这条边,所以其流量便是s->t的最小流
#include
using namespace std;
const int maxn = 100;
const int maxm = 10000;
const int INF = 0x3f3f3f3f;
int n,m,tot;
int l[maxn],h[maxn],cur[maxn];
struct edge{
int to,c,next;
edge(int x = 0, int y = 0, int z = 0) : to(x),c(y),next(z) {}
}es[maxm*2];
void add_edge(int u, int v, int c)
{
es[tot] = edge(v,c,h[u]);
h[u] = tot++;
es[tot] = edge(u,0,h[v]);
h[v] = tot++;
}
bool bfs(int s, int t)
{
memset(l,0,sizeof(l));
l[s] = 1;
queue q;
q.push(s);
while(!q.empty())
{
int u = q.front();
q.pop();
if(u == t) return true;
for(int i = h[u]; i != -1; i = es[i].next)
{
int v = es[i].to;
if(!l[v] && es[i].c)
{
l[v] = l[u] + 1;
q.push(v);
}
}
}
return false;
}
int dfs(int x, int t, int mf)
{
if(x == t) return mf;
int ret = 0;
for(int &i = cur[x]; i != -1; i = es[i].next)
{
int v = es[i].to;
if(es[i].c && l[v] == l[x] + 1)
{
int f = dfs(v,t,min(es[i].c,mf-ret));
es[i].c -= f;
es[i^1].c += f;
ret += f;
if(ret == mf) return ret;
}
}
return ret;
}
int dinic(int s, int t)
{
int ans = 0;
while(bfs(s,t))
{
for(int i = 0; i <= t; i++) cur[i] = h[i];
ans += dfs(s,t,INF);
}
return ans;
}
int in[maxn];
int fun(int s, int t, string str)
{
if(str[0] == '+') return s;
if(str[0] == '-') return t;
int res = 0;
for(int i = 0; i < str.size(); i++)
{
res *= 10;
res += str[i]-'0';
}
return res;
}
int main()
{
while(~scanf("%d%d",&n,&m) && (n||m))
{
tot = 0;
memset(h,-1,sizeof(h));
memset(in,0,sizeof(in));
int s = n+1, t = n+2, ss = 0, tt = t+1;
for(int i = 0; i < m; i++)
{
string s1,s2;
int u,v,p;
cin >> s1 >> s2;
scanf("%d",&p);
u = fun(s,t,s1);
v = fun(s,t,s2);
in[u] -= p;
in[v] += p;
add_edge(u,v,INF);
}
int res = 0;
for(int i = 1; i <= t; i++)
{
if(in[i] > 0)
{
res += in[i];
add_edge(ss,i,in[i]);
}
if(in[i] < 0) add_edge(i,tt,-in[i]);
}
int ans = dinic(ss,tt);
add_edge(t,s,INF);
ans += dinic(ss,tt);
if(res == ans) printf("%d\n",es[h[t]^1].c);
else printf("impossible\n");
}
return 0;
}