The same is the template problem.
First will have Yuanhui conversion to no Yuanhui, assuming the original Yuanhui for St, we join the Yuanhui for St, then we should from T to s even a flow for the +∞ edge, so that the original St to meet the balance of payments, degenerate into a common node.
Separating the necessary and other edges, from S to T run the maximum flow, all the edges connected to the source or sink are full and prove to be solvable.
Remove the t to s capacity for the +∞ edge, remove the necessary edges, and run the maximum stream from T to S.
Subtract the answers you get.
If the answer we get is negative, then it means that the internal T to S is connected to the ring, then we add s to s capacity for the-ans edge, run s to t maximum flow, so that all sides of the traffic should be 0, plus the lower bound of traffic is the answer.
1#include <bits/stdc++.h>2 #defineRep (I, A, b) for (int i = A; I <= b; i++)3 #defineDrep (I, A, b) for (int i = A; I >= b; i--)4 #defineREP (I, A, b) for (int i = A; I < b; i++)5 #defineMP Make_pair6 #definePB Push_back7 #defineCLR (x) memset (x, 0, sizeof (x))8 #defineXX First9 #defineyy secondTen using namespacestd; OnetypedefLong Longi64; Atypedef pair<int,int>PII; - Const intINF = ~0U>>1; - Consti64 INF = ~0ull >>1; the //****************************** - - Const intMAXN = the, MAXM =10105; - + structEd { - intu, V, NX, C; Ed () {} +Ed (int_u,int_v,int_NX,int_c): A u (_u), V (_v), NX (_NX), C (_c) {} at} E[MAXM <<1]; - intG[MAXN], Edtot =1; - voidAddedge (intUintVintc) { -E[++edtot] =Ed (U, V, g[u], c); -G[u] =Edtot; -E[++edtot] = Ed (V, u, G[v],0); inG[V] =Edtot; - } to + intLEVEL[MAXN], S, T; - BOOLBFsintSintt) { the Static intQUE[MAXN];intQh0), QT (0); *CLR (level); LEVEL[QUE[++QT] = s] =1; $ while(QH! =qt) {Panax Notoginseng intx = Que[++qh];if(QH = = MAXN-1) QH =0; - for(inti = g[x]; I i = e[i].nx)if(E[I].C &&!)LEVEL[E[I].V]) { theLEVEL[QUE[++QT] = e[i].v] = level[x] +1; + if(qt = = MAXN-1) qt =0; A } the } + return!!Level[t]; - } $ intDfsintUintRmintt) { $ if(U = = t)returnRM; - intRM1 =RM; - for(inti = G[u]; I i =e[i].nx) { the if(e[i].c && level[e[i].v] = = Level[u] +1) { - intFlow =dfs (E[I].V, min (e[i].c, RM), t);WuyiE[I].C-= flow, E[i ^1].C + =flow; the if(RM-= flow) = =0) Break; - } Wu } - if(Rm1 = = RM) Level[u] =0; About returnRM1-RM; $ } - - intL[MAXM],inch[MAXN]; - intMain () { A intN, M; +scanf"%d%d", &n, &m); theRep (I,1, M) { - intX, Y, a, B; scanf"%d%d%d%d", &x, &y, &a, &b); $L[i] = A *b; the if(b)inch[y] + = A,inch[x]-=A; theAddedge (x, y, a-a *b); the } theS = n +1, T = n +2; - intSum0); inRep (I,1, N) { the if(inch[I] >0) Sum + =inch[i]; the if(inch[I] >0) Addedge (S, I,inch[i]); About ElseAddedge (i, T,-inch[i]); the } theAddedge (N,1,0x3f3f3f3f); the while(BFS (S, T)) Sum-= DFS (s,0x3f3f3f3f, T); + if(sum) {Puts ("Impossible");return 0; } - intAns =e[edtot].c; thee[edtot].c = E[edtot ^1].C =0;Bayi inttmp0); the while(BFS (N,1)) tmp + = DFS (n,0x3f3f3f3f,1); theAns-=tmp; - if(Ans <0) { -Addedge (S),1, -ans); the while(BFS (S, N)) Dfs (s,0x3f3f3f3f, n); theAns =0; the } theprintf"%d\n", ans); -Rep (I,1, M) { theprintf (i = =1?"%d":"%d", e[(i <<1) ^1].C +l[i]); the } thePuts"");94 return 0; the}
View Code
sgu176 Flow Construction "There are Yuanhui with the lowest flow in the upper and lower bounds"