Title Link: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1314
Test instructions: Give the direction graph of n points, M edge, and the upper and lower bounds of each edge, ask if there is a feasible flow satisfies the condition, if the output Yes is satisfied and the traffic of each edge is output.
If output no is not satisfied.
According to Zhou Yuan's " a simple method to solve the network flow problem in the network with upper and lower bounds of traffic "
The practice of non-Yuanhui upper and lower boundary network streams is:
The lower bound of the Edge U->v is B (u,v), and the upper bound is C (u,v).
Set M (i) to the lower bound sum of the inflow I for I nodes-the lower bound sum of the outflow I.
Add source point S and meeting point T.
If M (i) >=0 the Edge s->i, the capacity is M (i).
If M (i) <0 the Edge i->t, the capacity is-M (i).
For the original Edge u-v in the diagram, the Edge u->v, the capacity is C (u,v)-B (u,v).
Then the maximum flow is calculated. If all the edges from the source point are full-flow, then there is a viable flow that satisfies the condition.
1#include <bits/stdc++.h>2 using namespacestd;3 intN, M;4 #defineMAXN 2105 Const intINF =0x3f3f3f3f;6 structEdge7 {8 int from, to, cap, flow;9Edge (intFintTintCintFL)Ten { One from= f; to = t; Cap = C; Flow =FL; A } - }; -Vector <Edge>edges; theVector <int>G[MAXN]; - intCUR[MAXN], VIS[MAXN], D[MAXN]; - intN, m, S, T; - voidAddedge (int from,intTo,intcap) + { -Edges.push_back (Edge ( from, to, Cap,0)); +Edges.push_back (Edge to, from,0,0)); Am =edges.size (); atg[ from].push_back (M-2); -G[to].push_back (M-1); - } - BOOLBFS () - { -memset (Vis,0,sizeof(Vis)); inVis[s] =1; -D[s] =0; toQueue <int>Q; + Q.push (s); - while(!q.empty ()) the { * intU =Q.front (); Q.pop (); $ for(inti =0; I < g[u].size (); i++)Panax Notoginseng { -Edge &e =Edges[g[u][i]]; the if(!vis[e.to] && e.cap >E.flow) + { AVis[e.to] =1; theD[e.to] = d[u]+1; + Q.push (e.to); - } $ } $ } - returnVis[t]; - } the intDfsintXinta) - {Wuyi if(x = = T | | a = =0)returnA; the intFlow =0, F; - for(int&i = Cur[x]; I < g[x].size (); i++) Wu { -Edge &e =Edges[g[x][i]]; About if(d[x]+1= = D[e.to] && (f = dfs (e.to, min (E.cap-e.flow, a)) >0) $ { -E.flow + =F; -edges[g[x][i]^1].flow-=F; -Flow + =F; AA-=F; + if(A = =0) Break; the } - } $ returnflow; the } the intMaxflow () the { the intFlow =0; - while(BFS ()) in { thememset (cur,0,sizeof(cur)); theFlow + =DFS (s, INF); About } the returnflow; the } the intT; + int inch[MAXN], out[MAXN], MI[MAXN]; - intlow[ About* About]; the intMain ()Bayi { thescanf"%d", &T); the while(t--) - { - edges.clear (); the for(inti = s; I <= t; i++) g[i].clear (); thescanf"%d%d", &n, &M); thes =0; t = n+1; theMemsetinch,0,sizeof(inch)); -Memset out,0,sizeof( out)); the for(inti =1; I <= M; i++) the { the intu, V, L, F;94scanf"%d%d%d%d", &u, &v, &l, &f); theLow[i] =l; theAddedge (U, V, fl); the out[u] + =l;98 inch[V] + =l; About } - for(inti =1; I <= N; i++)101 {102Mi[i] =inch[I]- out[i];103 if(Mi[i] >=0) Addedge (S, I, Mi[i]);104 ElseAddedge (i, T,-mi[i]); the }106 intFlow =Maxflow ();107Vector <int>ans;108 BOOLFlag =true;109 for(inti =0; I < m; i+=2) the {111 if(Edges[i]. from= = S && edges[i].cap >Edges[i].flow) the {113Flag =false; Break; the } the Else if(Edges[i]. from! = S && edges[i].to! =t) the {117 Ans.push_back (edges[i].flow);118 }119 } - if(flag)121 {122printf"yes\n");123 for(inti =0; I < ans.size (); i++)124 { theprintf"%d\n", ans[i]+low[i+1]);126 }127 } - Elseprintf"no\n");129 } the return 0;131}
ZOJ 2314 Reactor Cooling no Yuanhui has upper and lower bounds network flow