SGU 176 flow Construction (with Yuanhui upper and lower bounds minimum flow)

Source: Internet
Author: User

"Topic link"

http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=11025

Model

There is a minimum flow of Yuanhui points in the upper and lower bounds. The minimum flow that satisfies both the upper and lower bounds and the flow balance.

Ideas

Construct the network according to the feasible flow. Not even the edge of the t->s. Add the maximum flow of the Yuanhui point, and then even t->s an INF edge and run the maximum stream over the residual network. The first time to seek the maximum flow so that can walk the edge is full, the second time to seek additional Yuanhui point maximum flow t->s flow will be as small as possible.

It is also possible to divide the lower bound mid, then the Edge (T,s,mid), if there is a viable flow, the mid is feasible.

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 = 5e4+Ten; the Const intINF =1e9; -  - ll Read () { -     CharC=GetChar (); +ll f=1, x=0; -      while(!IsDigit (c)) { +         if(c=='-') f=-1; C=GetChar (); A     } at      while(IsDigit (c)) -x=x*Ten+c-'0', c=GetChar (); -     returnx*F; - } -  - structEdge { in     intU,v,cap,flow; -Edge (intu=0,intv=0,intcap=0,intflow=0) to : U (u), V (v), cap (CAP), flow (flow) {} + }; - structDinic { the     intn,m,s,t; *     intD[n],cur[n],vis[n]; $vector<int>G[n];Panax NotoginsengVector<edge>es; -queue<int>Q; the     voidInitintN) { +          This->n=N; A es.clear (); thefor (I,0, N) g[i].clear (); +     } -     voidClear () { $for (I,0,(int) Es.size ()-1) es[i].flow=0; $     } -     voidAddedge (intUintVintW) { -Es.push_back (Edge (U,v,w,0)); theEs.push_back (Edge (V,u,0,0)); -m=es.size ();WuyiG[u].push_back (M-2); theG[v].push_back (M-1); -     } Wu     intBFs () { -memset (Vis,0,sizeof(Vis)); AboutQ.push (s); d[s]=0; vis[s]=1; $          while(!Q.empty ()) { -             intu=Q.front (); Q.pop (); -for (I,0,(int) G[u].size ()-1) { -edge& e=Es[g[u][i]]; A                 intv=e.v; +                 if(!vis[v]&&e.cap>e.flow) { thevis[v]=1; -d[v]=d[u]+1; $ Q.push (v); the                 } the             } the         } the         returnVis[t]; -     } in     intDfsintUinta) { the         if(U==t| |! AreturnA; the         intflow=0, F; About          for(int& I=cur[u];i<g[u].size (i++);) { theedge& 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) { +e.flow+=F; -es[g[u][i]^1].flow-=F; theFlow+=f; a-=F;Bayi                 if(!a) Break; the             } the         } -         returnflow; -     } the     intMaxflow (intSintt) { the          This->s=s, This->t=T; the         intflow=0; the          while(BFS ()) { -memset (cur,0,sizeof(cur)); theflow+=DFS (s,inf); the         } the         returnflow;94     } the } DC; the  the intN,m,sum,down[n],inch[n],id[n];98  About intMain () - {101      while(SCANF ("%d%d", &n,&m) = =2) {102sum=0;103         ints=0, t=n+1;104Dc.init (t+2); theMemsetinch,0,sizeof(inch));106for (I,1, M) {107id[i]=-1;108             intX=read (), Y=read (), Z=read (), c=read ();109             if(c) down[i]=z,sum+=Z,DC. Addedge (s,y,z), DC. Addedge (x,t,z); the             Elsedown[i]=0, DC. Addedge (x, Y, z), Id[i]=dc.es.size ()-2;111         } the         intflow=DC. Maxflow (s,t);113dc. Addedge (N,1, INF); theflow+=DC. Maxflow (s,t); the         if(Flow!=sum) puts ("Impossible"); the         Else {117printf"%d\n", Dc.es[dc.es.size ()-2].flow);118for (I,1, M-1) 119                 if(id[i]>=0) printf ("%d", dc.es[id[i]].flow+down[i]); -                 Elseprintf"%d", Down[i]);121             if(id[m]>=0) printf ("%d\n", dc.es[id[m]].flow+down[m]);122             Elseprintf"%d\n", Down[m]);123         }124     } the     return 0;126}

SGU 176 flow Construction (with Yuanhui upper and lower bounds minimum flow)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.