Title: Acdream 1211 Reactor Cooling
Classification: There are upper and lower bounded network flows without sinks.
Test instructions
Give N a point. and m root pipe, each pipe used to flow the liquid. One-way. Every moment each pipe flows in the material to be equal to the outflow of material, to make the m pipe to form a loop body. Inside the flow of material.
And to meet The flow limit for each pipe, the range is [Li,ri]. in order to meet the flow in each moment can not exceed Ri ( maximum flow problem ) . At the same time the minimum cannot be less than Li.
Like what:
(4 points,6 pipe)
1 3 (the upper bound of 1->2 is 3, the lower bound is 1)
1 3
3 4 1 3
4 1 1 3
1 3 1 3
4 2 1 3
Feasible flow:
Another example: if the upper bound of all pipes is 2 Nether 1 , then a feasible flow cannot be obtained.
Exercises
The upper bound is represented by CI , and the nether is represented by bi .
The nether must be full, so for each edge, after removing the Nether, its free stream is ci–bi.
Main idea: flow in and out of each point = stream flowing out
For each point i, make
mi= sum ( the Nether stream in which I point all flows ) –sum ( the Nether Stream where I point all flows out )
Assuming that the mi is greater than 0and that the point must also flow out of the free stream to mi , then we connect a single mi edge from the source point to that point.
Assuming that Mi is less than 0, this point must also flow in The free stream of MI. So we'll go from that point to the edge of a Mi to the meeting point.
Suppose the maximum flow of the s->t is calculated to see if the full stream ( The neighboring edge of S is full ).
Marue have the solution, otherwise there is no solution.
AC Code:
#include <cstdio> #include <cstring> #include <string> #include <iostream> #include < algorithm> #include <vector> #include <map> #include <queue> #define Del (b) memset (A,b,sizeof (a) ) using namespace Std;const int inf = 0x3f3f3f3f;const int N = 250;struct node{int from,to,cap,flow;}; Vector<int> v[n];vector<node> E;int Vis[n]; Build the hierarchy diagram int cur[n];void add_node (int from,int to,int cap) {E.push_back ((Node) {from,to,cap,0}); E.push_back (Node) {to,from,0,0}); int tmp=e.size (); V[from].push_back (tmp-2); V[to].push_back (tmp-1);} BOOL BFs (int s,int t) {Del (vis,-1); Queue<int> Q; Q.push (s); Vis[s] = 0; while (!q.empty ()) {int X=q.front (); Q.pop (); for (int i=0;i<v[x].size (); i++) {Node TMP = e[v[x][i]]; if (vis[tmp.to]<0 && tmp.cap>tmp.flow)//The second condition guarantees {vis[tmp.to]=vis[x]+1; Q.push (tmp.to); }}} if (vis[t]>0) return true; return false;} int dfs (int o,int f,int t) {if (o==t | | f==0)//optimize return F; int a = 0,ans=0; for (int &i=cur[o];i<v[o].size (); i++)//Note front ' & ', very important optimization {Node &tmp = e[v[o][i]]; if (vis[tmp.to]== (vis[o]+1) && (a = DFS (Tmp.to,min (f,tmp.cap-tmp.flow), T)) >0) {tmp.flow+=a; E[v[o][i]^1].flow-=a; Ans+=a of the mode of storage and mapping; F-=a; if (f==0)//attention optimization break; }} return ans; Optimized}int dinci (int s,int t) {int ans=0; while (BFS (s,t)) {Del (cur,0); int Tm=dfs (S,INF,T); Ans+=tm; } return ans; void Mp_clear (int n) {for (int i=0;i<=n;i++) v[i].clear (); E.clear ();} int come[n],to[n];int flow[n][n];struct node1{int x, y;} Num[n*n];int Main () {int n,m; int T; scanf ("%d", &t); while (t--) {scanf ("%d%d", &n,&m); Del (come,0); Del (to,0); Del (flow,0); int s=0,t = n+1; for (int i=0;i<m;i++) {int x,y,mi,ma; scanf ("%d%d%d%d", &x,&y,&mi,&ma); Num[i] = (Node1) {x, y}; Flow[x][y] + = mi; Add_node (X,Y,MA-MI); come[x]+= mi; To[y] + = mi; } int count=0; for (int i=1;i<=n;i++) {int tmp = Come[i]-to[i]; if (tmp<0) {count+=tmp; Add_node (S,I,-TMP); } if (tmp>0) Add_node (i,t,tmp); } count =-count; int ans = Dinci (s,t); if (ans! = count) puts ("NO"); else {puts ("YES"); for (int i=1;i<=n;i++) {for (int j=0;j<v[i].size (); j + +) { int f = v[i][j]; Flow[e[f].from][e[f].to]+=abs (E[f].flow); printf ("xx%d%d%d \ n ", E[f].from,e[f].to,e[f].flow); }} for (int i=0;i<m;i++) {printf ("%d\n", Flow[num[i].x][num[i].y]); }} if (T) puts (""); Mp_clear (t); } return 0;}
Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.
Acdream 1211 Reactor Cooling "Boundary network traffic + output traffic"