Topic links
Test instructions: There are n (1 <= n <= 200) points in the direction graph, with no self-loop or a ring size of at least 3. Given the minimum and maximum flow per edge, what is the feasible flow per edge?
Idea: The general solution of the network flow does not consider the nether problem, that is, the flow can be 0, when there is a lower bound, we only need to change the upper bound into r-l, it is also necessary to meet the traffic conservation, increase the source point s and sinks T, when the point U inflow is greater than the outflow, the point U and S edge, Similarly, when u outflow is greater than inflow, the outflow flow is connected to the meeting point T; direct running maximum flow;
Note: At this time to determine whether there is a feasible solution, is to see whether all the edges of the source point is full flow , full flow of the internal flow of the balance, there is a feasible solution; then offline for the ID output;
PS: For the network flow problem, whether the topic is a direction or no map, because it is augmented search, so it must be built without the map.
#include <bits/stdc++.h>using namespacestd;#defineRep0 (I,L,R) for (int i = (l); i < (R); i++)#defineREP1 (I,L,R) for (int i = (l); I <= (r); i++)#defineRep_0 (i,r,l) for (int i = (r); i > (l); i--)#defineRep_1 (i,r,l) for (int i = (r); I >= (l); i--)#defineMS0 (a) memset (A,0,sizeof (a))#defineMS1 (a) memset (A,-1,sizeof (a))#defineMSi (a) memset (A,0x3f,sizeof (a))#defineINF 0x3f3f3f3f#defineLson L, M, RT << 1#defineRson m+1, R, RT << 1|1#defineLowbit (x) (x& (×))typedef pair<int,int>PII;#defineA First#defineB Second#defineMK Make_pairtypedefLong Longll;typedef unsignedint UINT;inti,j,k,n,m;Const intM =40000;intHead[m],tot;structedge{int from, To,cap,flow,next,id; Edge () {} Edge (intFintTo,intCapintNext,intID): from(f), to, Cap (CAP), next (next), ID (ID), Flow (0) {}}e[m<<1];inlinevoidInsintUintVintWintID) {e[++tot] =Edge{u,v,w,head[u],id}; Head[u]=tot;}ints,t,cur[m],d[222];intque[222];BOOLBFS () {rep1 (i,s,t) d[i]= -1; intL =1, r =1; Que[r+ +] = s;d[s] =0; while(L < R) {//[L,r] intU = que[l++]; for(inti = Head[u];i;i =E[i]. Next) {intv =e[i].to; if(D[v] <0&& e[i].cap > E[i].flow) {//only the arc of the residual network is consideredD[V] = D[u] +1;//to flutter out of a path;que[r++] =v; if(v = = t)return true; } } } return false;}intDFS (intXintA//a indicates the minimum residual amount of all arcs so far{ if(x = = T | | a = =0)returnA; int& i = cur[x];//Dfs to the same point multiple times when backtracking if(i = =0) i =Head[x]; intFlow =0, F; for(; i;i = E[i]. Next) {//starting with the last thought of the arc intv =e[i].to; if(D[v] = = d[x]+1&& (f = DFS (V,min (A,e[i].cap-e[i].flow))) >0) {E[i].flow+=F; E[i^1].flow-=F; Flow+=F; A-= f;//Residual quantity-flow if(A = =0) Break; } } returnflow;}intDinic () {intFlow =0; while(BFS ()) {//there are still augmented roads when DFSREP1 (i,s,t) cur[i] =0;//record the number of arcs of the currently explored pointFlow + =DFS (S,inf); } returnflow;}intL[m],ans[m];intMain () {//freopen ("Data.txt", "R", stdin); //freopen ("OUT.txt", "w", stdout); intKase =0; while(SCANF ("%d%d", &n,&m) = =2){ if(kase++) puts (""); MS0 (head); Tot=1; MS0 (d); intU,v,r; S=0, t = n+1; REP1 (i,1, M) {scanf ("%d%d%d%d",&u,&v,&l[i],&R); D[u]-=L[i]; D[V]+=L[i]; INS (U,v,r-l[i],i); Ins (V,u,0,0); } rep1 (I,1, N) { if(D[i] >0) ins (S,i,d[i],0), Ins (I,s,0,0); if(D[i] <0) ins (I,t,-d[i],0), Ins (T,i,0,0); } dinic (); BOOLFlag =false; for(intd = Head[s];d; d =E[d]. Next) {if(E[d].cap-e[d].flow) {flag =true; Break;} } if(flag) {Puts ("NO");Continue;} Puts ("YES"); REP1 (i,2, tot)if(e[i].id) {ans[e[i].id]= e[i].flow+L[e[i].id]; } rep1 (I,1, m) printf ("%d\n", Ans[i]); } return 0;}
SGU 194 Reactor Cooling dinic to solve the maximum flow of a passive non-sink with upper and lower bounds