Crazy Circuits
Time Limit: 2000ms
Memory Limit: 32768KB
This problem will be judged on
HDU. Original ID: 315764-bit integer IO format: %I64d Java class name: Main
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 problem1). 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?
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 problem1). 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).
![](http://www.bnuoj.com/v3/data/images/C239-1009-1.jpg)
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
Mlines each contain a description of some component in the diagram. The
ith 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
Source
解题:有源汇的上下界最小流
- 构造附加网络,不添加[T,S]边
- 对[SS,TT]求最大流
- 添加[T,S]边
- 对[SS,TT]求最大流,若满流,则边[T,S]上的流量即是最小流
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 #include <bits/stdc++.h> 2 using namespace std; 3 const int INF = 0x3f3f3f3f; 4 const int maxn = 100; 5 struct arc { 6 int to,flow,next; 7 arc(int x = 0,int y = 0,int z = -1) { 8 to = x; 9 flow = y; 10 next = z; 11 } 12 } e[maxn*maxn]; 13 int head[maxn],cur[maxn],d[maxn],du[maxn],tot; 14 void add(int u,int v,int flow) { 15 e[tot] = arc(v,flow,head[u]); 16 head[u] = tot++; 17 e[tot] = arc(u,0,head[v]); 18 head[v] = tot++; 19 } 20 bool bfs(int S,int T) { 21 queue<int>q; 22 q.push(S); 23 memset(d,-1,sizeof d); 24 d[S] = 1; 25 while(!q.empty()) { 26 int u = q.front(); 27 q.pop(); 28 for(int i = head[u]; ~i; i = e[i].next) { 29 if(e[i].flow && d[e[i].to] == -1) { 30 d[e[i].to] = d[u] + 1; 31 q.push(e[i].to); 32 } 33 } 34 } 35 return d[T] > -1; 36 } 37 int dfs(int u,int T,int low) { 38 if(u == T) return low; 39 int a,tmp = 0; 40 for(int &i = cur[u]; ~i; i = e[i].next) { 41 if(e[i].flow && d[e[i].to] == d[u]+1&&(a=dfs(e[i].to,T,min(e[i].flow,low)))) { 42 e[i].flow -= a; 43 e[i^1].flow += a; 44 low -= a; 45 tmp += a; 46 if(!low) break; 47 } 48 } 49 if(!tmp) d[u] = -1; 50 return tmp; 51 } 52 int Dinic(int S,int T,int ret = 0) { 53 while(bfs(S,T)) { 54 memcpy(cur,head,sizeof head); 55 ret += dfs(S,T,INF); 56 } 57 return ret; 58 } 59 int main() { 60 char a[10],b[10]; 61 int n,m,u,v,bound,S,T,SS,TT; 62 while(scanf("%d%d",&n,&m),n||m) { 63 memset(head,-1,sizeof head); 64 memset(du,0,sizeof du); 65 int sum = S = 0; 66 T = n + 1; 67 SS = T + 1; 68 TT = SS + 1; 69 for(int i = tot = 0; i < m; ++i) { 70 scanf("%s%s%d",a,b,&bound); 71 if(a[0] == '+') u = S; 72 else sscanf(a,"%d",&u); 73 if(b[0] == '-') v = T; 74 else sscanf(b,"%d",&v); 75 add(u,v,INF); 76 du[u] -= bound; 77 du[v] += bound; 78 } 79 for(int i = 0; i <= T; ++i) { 80 if(du[i] > 0) { 81 add(SS,i,du[i]); 82 sum += du[i]; 83 } else add(i,TT,-du[i]); 84 } 85 u = Dinic(SS,TT); 86 add(T,S,INF); 87 v = Dinic(SS,TT); 88 if(u + v == sum) printf("%d\n",e[tot-1].flow); 89 else puts("impossible"); 90 } 91 return 0; 92 }