Description
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). 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?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 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 0Sample Output
9 impossibleHint
For those who are electronics-inclined, imagine that you have the ability to adjust the potential on any component without 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.
【分析】
就是一个有源汇、有上下界的最小流。汇点源点连一条INF的边变成循环流,新增超级源点、超级汇点,拆边,跑最大流判满流。这题不用拆边再反过来跑一遍求最小流,因为所有边的上届都是INF。
代码如下:
1 #include<cstdio> 2 #include<cstdlib> 3 #include<cstring> 4 #include<iostream> 5 #include<algorithm> 6 #include<queue> 7 using namespace std; 8 #define Maxn 1010 9 #define Maxm 100100 10 #define INF 0xfffffff 11 12 char s[110]; 13 int stt,edd; 14 int first[Maxn],dis[Maxn]; 15 int sum; 16 17 struct node 18 { 19 int x,y,f,o,next; 20 }t[Maxm];int len; 21 22 int mymin(int x,int y) {return x<y?x:y;} 23 24 void ins(int x,int y,int f) 25 { 26 if(x==stt) sum+=f; 27 t[++len].x=x;t[len].y=y;t[len].f=f; 28 t[len].next=first[x];first[x]=len;t[len].o=len+1; 29 t[++len].x=y;t[len].y=x;t[len].f=0; 30 t[len].next=first[y];first[y]=len;t[len].o=len-1; 31 } 32 33 void make_edge(int x,int y,int k1,int k2) 34 { 35 ins(stt,y,k1); 36 ins(x,edd,k1); 37 ins(x,y,k2-k1); 38 } 39 40 queue<int > q; 41 bool bfs(int st,int ed) 42 { 43 while(!q.empty()) q.pop(); 44 memset(dis,-1,sizeof(dis)); 45 q.push(st);dis[st]=0; 46 while(!q.empty()) 47 { 48 int x=q.front(); 49 for(int i=first[x];i;i=t[i].next) if(t[i].f>0) 50 { 51 int y=t[i].y; 52 if(dis[y]==-1) 53 { 54 dis[y]=dis[x]+1; 55 q.push(y); 56 } 57 } 58 q.pop(); 59 } 60 if(dis[ed]!=-1) return 1; 61 return 0; 62 } 63 64 int ffind(int x,int ed,int flow) 65 { 66 if(x==ed) return flow; 67 int now=0; 68 for(int i=first[x];i;i=t[i].next) if(t[i].f>0) 69 { 70 int y=t[i].y; 71 if(dis[y]==dis[x]+1) 72 { 73 int a=ffind(y,ed,mymin(flow-now,t[i].f)); 74 t[i].f-=a; 75 t[t[i].o].f+=a; 76 now+=a; 77 if(now==flow) break; 78 } 79 } 80 if(now==0) dis[x]=-1; 81 return now; 82 } 83 84 int max_flow(int st,int ed) 85 { 86 int ans=0; 87 while(bfs(st,ed)) 88 { 89 ans+=ffind(st,ed,INF); 90 } 91 if(ans!=sum) return -1; 92 return ans; 93 } 94 95 int main() 96 { 97 int n,m; 98 while(1) 99 { 100 scanf("%d%d",&n,&m); 101 if(n==0&&m==0) break; 102 int st=n+1,ed=st+1;len=0; 103 stt=ed+1;edd=stt+1;sum=0; 104 memset(first,0,sizeof(first)); 105 for(int i=1;i<=m;i++) 106 { 107 int x,y; 108 scanf("%s",s); 109 if(s[0]=='+') x=st; 110 else if(s[0]=='-') x=ed; 111 else 112 { 113 int now=0;x=0; 114 while(s[now]>='0'&&s[now]<='9') x=x*10+s[now++]-'0'; 115 }scanf("%s",s); 116 if(s[0]=='+') y=st; 117 else if(s[0]=='-') y=ed; 118 else 119 { 120 int now=0;y=0; 121 while(s[now]>='0'&&s[now]<='9') y=y*10+s[now++]-'0'; 122 } 123 int c; 124 scanf("%d",&c); 125 make_edge(x,y,c,INF); 126 } 127 int id=len+2; 128 ins(ed,st,INF); 129 int now=max_flow(stt,edd); 130 if(now==-1) printf("impossible "); 131 else 132 { 133 printf("%d ",t[id].f); 134 } 135 } 136 return 0; 137 }
2016-06-13 13:12:16