Network flow templates

Source: Internet
Author: User

Ford_fulkerson (O (f*e))

Don't feel like it, but just write it ...

Complexity: The maximum flow is F, at least 1 at a time, up to the augmented F, each time up to the E-bar (the entire picture).

#include <cstdio>#include<cstring>#include<vector>#include<algorithm>#defineRep (i,n) for (int i=0;i< (n); i++)#defineFor1 (i,a,n) for (int i= (a); i<= (n); i++)#defineCC (i,a) memset (i,a,sizeof (i))#defineRead (a) A=getnum ()#definePrint (a) printf ("%d", a)using namespacestd;Const intmaxn=205, inf=0x7fffffff;structEdge {intTo,cap,rev;};intM,n;BOOLVis[maxn];vector<edge>G[maxn];inlineintGetnum () {intR=0, k=1;CharC for(C=getchar ();c<'0'|| C>'9'; C=getchar ())if(c=='-') k=-1; for(; c>='0'&&c<='9'; C=getchar ()) r=r*Ten+c-'0';returnr*K;}voidAdd_edge (int  from,intTo,intcap) {g[ from].push_back (Edge) {to,cap,g[to].size ()}); G[to].push_back (Edge) { from,0, g[ from].size ()-1 });}intDfsintVintTintf) {    if(v==t)returnF; VIS[V]=true; Rep (I,g[v].size ()) {Edge&e=G[v][i]; if(!vis[e.to]&&e.cap>0)        {            intD=Dfs (E.to,t,min (f,e.cap)); if(d>0) {E.cap-=D; G[e.to][e.rev].cap+=D; returnD; }        }    }    return 0;}intMax_flow (intSintt) {CC (Vis,false); intflow=0, F;  while((F=dfs (s,t,inf)) >0) {Flow+=F; CC (Vis,false); }    returnflow;}voidinit () {read (m); Read (n); For1 (i,1, M) {        int  from, To,cap; Read ( from); Read (to);        Read (CAP); Add_edge ( from, To,cap); }}intMain () {init (); Print (Max_flow (1, N)); return 0;} 

Dinic (O (e*v^2))

Complexity: Each level chart runs, indicating the shortest augmentation path to become longer, re-BFS to construct a new hierarchical map of the shortest augmented road at least +1, the length of up to V-1, so there are up to (V-1) a hierarchy of graphs. Each of the augmented paths has at least one bottleneck in each hierarchy (the Small arc), after running the shortest circuit, the arc is gone (because the reverse arc does not conform to the level, will not go in this level chart), up to the e-arc, at least one elimination at a time, that up to run e times, each time up to run the V point, so in a hierarchy chart up to run E * V times, comprehensive, total complex The degree is O (e*v^2).

#include <cstdio>#include<cstring>#include<vector>#include<queue>#include<algorithm>#defineRep (i,n) for (int i=0;i<n;i++)#defineFor1 (i,a,n) for (int i= (a); i<= (n); i++)#defineCC (i,a) memset (i,a,sizeof (i))#defineRead (a) A=getnum ()#definePrint (a) printf ("%d\n", a)using namespacestd;Const intmaxn=205, inf=0x7fffffff;intM,n;intITER[MAXN],LEVEL[MAXN];structEdge {intTo,cap,rev;}; Vector<edge>G[maxn];inlineintGetnum () {intR=0, k=1;CharC for(C=getchar ();c<'0'|| C>'9'; C=getchar ())if(c==-'-') k=-1; for(; c>='0'&&c<='9'; C=getchar ()) r=r*Ten+c-'0';returnr*K;}voidAdd_edge (int  from,intTo,intcap) {g[ from].push_back (Edge) {to,cap,g[to].size ()}); G[to].push_back (Edge) { from,0, g[ from].size ()-1 });}voidBFsints) {CC (level,-1); Level[s]=1; Queue<int>Q;    Q.push (s);  while(!Q.empty ()) {        intt=Q.front (); Q.pop (); Rep (I,g[t].size ()) {Edge&e=G[t][i]; if(level[e.to]<0&&e.cap>0) {level[e.to]=level[t]+1;            Q.push (e.to); }        }    }}intDfsintVintTintf) {    if(v==t)returnF;  for(int&i=iter[v];i<g[v].size (); i++) {Edge&e=G[v][i]; if(e.cap>0&&level[v]<Level[e.to]) {            intD=Dfs (E.to,t,min (f,e.cap)); if(d>0) {E.cap-=D; G[e.to][e.rev].cap+=D; returnD; }        }    }    return 0;}intMax_flow (intSintt) {    intflow=0;    BFS (s);  while(level[t]>0) {CC (ITER,0); intF;  while((F=dfs (s,t,inf)) >0) flow+=F;    BFS (s); }    returnflow;}voidinit () {read (m); Read (n); For1 (i,1, M) {        int  from, To,cap; Read ( from); Read (to);        Read (CAP); Add_edge ( from, To,cap); }}intMain () {init (); Print (Max_flow (1, N)); return 0;} 

Network flow templates

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.