"Topic link"
http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=20757
Test instructions
The feasible flow of passive and non-sinks with the upper and lower bounds of capacity is obtained.
Ideas
The passive non-sink feasible flow requires that all vertices meet the flow balance.
The basic idea is to convert to the maximum flow to do.
For edges (u,v,b,c), The Edge (U,v,c-b). In order to maintain the flow balance, we also need to connect the edge
1. (S,u,inb[u]-outb[u]) Inb>outb
2. (U,t,outb[u]-inb[u]) outb>inb
S->t run the maximum flow, if the out side of S or T's on the side of the full load there is a solution.
Code
1#include <Set>2#include <cmath>3#include <queue>4#include <vector>5#include <cstdio>6#include <cstring>7#include <iostream>8#include <algorithm>9 #defineTrav (u,i) for (int i=front[u];i;i=e[i].nxt)Ten #definefor (A,B,C) for (int a= (b); a<= (c); a++) One using namespacestd; A -typedefLong Longll; - Const intN = 4e2+Ten; the Const intM = n*n+Ten; - Const intINF =1e9; - - ll Read () { + CharC=GetChar (); -ll f=1, x=0; + while(!IsDigit (c)) { A if(c=='-') f=-1; C=GetChar (); at } - while(IsDigit (c)) -x=x*Ten+c-'0', c=GetChar (); - returnx*F; - } - in structEdge { - intU,v,cap,flow; to }; + structDinic { - intn,m,s,t; the intD[n],cur[n],vis[n]; *vector<int>G[n]; $Vector<edge>es;Panax Notoginsengqueue<int>Q; - voidInitintN) { the This->n=N; + es.clear (); Afor (I,0, N) g[i].clear (); the } + voidClear () { -for (I,0,(int) Es.size ()-1) es[i].flow=0; $ } $ voidAddedge (intUintVintW) { -Es.push_back (Edge) {u,v,w,0}); -Es.push_back (Edge) {v,u,0,0}); them=es.size (); -G[u].push_back (M-2);WuyiG[v].push_back (M-1); the } - intBFs () { Wumemset (Vis,0,sizeof(Vis)); -Q.push (s); d[s]=0; vis[s]=1; About while(!Q.empty ()) { $ intu=Q.front (); Q.pop (); -for (I,0,(int) G[u].size ()-1) { -edge& e=Es[g[u][i]]; - intv=e.v; A if(!vis[v]&&e.cap>e.flow) { +vis[v]=1; thed[v]=d[u]+1; - Q.push (v); $ } the } the } the returnVis[t]; the } - intDfsintUinta) { in if(U==t| |! AreturnA; the intflow=0, F; the for(int& I=cur[u];i<g[u].size (i++);) { Aboutedge& e=Es[g[u][i]]; the intv=e.v; the if(d[v]==d[u]+1&& (F=dfs (V,min (a,e.cap-e.flow)) >0) { thee.flow+=F; +es[g[u][i]^1].flow-=F; -Flow+=f; a-=F; the if(!a) Break;Bayi } the } the returnflow; - } - intMaxflow (intSintt) { the This->s=s, This->t=T; the intflow=0; the while(BFS ()) { thememset (cur,0,sizeof(cur)); -flow+=DFS (s,inf); the } the returnflow; the }94 } DC; the the intN,m,sum,b[m],inb[n],outb[n]; the 98 intMain () About { -N=read (), m=read ();101Dc.init (n+4);102 ints=0, t=n+1;103for (I,1, M) {104 intU=read (), V=read (), B=read (), c=read (); thedc. Addedge (u,v,c-b);106b[i]=b;107inb[v]+=b,outb[u]+=b;108 }109for (I,1, N) { the intc=inb[i]-Outb[i];111 if(c>0) DC. Addedge (s,i,c), sum+=C; the Elsedc. Addedge (i,t,-c);113 } the if(DC. Maxflow (s,t)!=sum) puts ("NO"); the Else { thePuts"YES");117for (I,0, M-1) printf ("%d\n", dc.es[i*2].flow+b[i+1]);118 }119 return 0; -}
SGU 194 Reactor Cooling (passive non-sink viable flow with capacity upper and lower bounds)