Network flow--Maximum flow dinic template

Source: Internet
Author: User

The standard large petition template, except the variable name is not the same ... Only the Init function, the Add function, and the MF function are required in the main function

1#include <stdio.h>//I 'm going to add all these files.2#include <string.h>3#include <queue>4#include <vector>5#include <algorithm>6 using namespacestd;7 Const intmaxm= Max+5;//total number of points8 Const intinf=0x3f3f3f3f;9 Ten structedge{//structure of arcs, variables: Starting point, end point, capacity, flow of Arc One     int  from, to,c,f; AEdgeintAintBintMintN): from(a), to (b), C (M), f (n) {} - }; -  the structdinic{ -     intM,s,t;//number of edges, source point label, meeting Point label -vector<edge>e;//side -vector<int>g[maxm];//G[i][j] denotes the number of the J-side in E, starting at point I +     BOOLVIS[MAXM]; -     intD[MAXM],CUR[MAXM];//D is the source-to-point distance, Cur is the currently traversed edge +     voidInitintN) {initialize, n is the number of points (designator 0~n-1) A          for(intI=0; i<n+5; i++) g[i].clear (); at e.clear (); -     } -     voidAddintAintBintV) {//adding arcs and reverse arcs -E.push_back (Edge (A,b,v,0));//forward Arc capacity V, Reverse arc capacity 0 -E.push_back (Edge (B,a,0,0)); -m=e.size (); inG[a].push_back (M-2); -G[b].push_back (M-1); to     } +     BOOLBFs () { -memset (Vis,0,sizeof(Vis)); thequeue<int>Q; * Q.push (s); $d[s]=0;Panax Notoginsengvis[s]=1; -          while(!Q.empty ()) { the             intu=Q.front (); Q.pop (); +              for(intI=0; I<g[u].size (); i++){ AEdge tmp=E[g[u][i]]; the                 if(!vis[tmp.to]&&tmp.c>tmp.f) { +vis[tmp.to]=1; -d[tmp.to]=d[u]+1; $ Q.push (tmp.to); $                 } -             } -         } the         returnVis[t]; -     }Wuyi     intDfsintXinta) { the         if(x==t| | a==0)returnA; -         intflow=0, F; Wu          for(int& I=cur[x];i<g[x].size (i++);){ -Edge &tmp=E[g[x][i]]; About             if(d[x]+1==d[tmp.to]&& (F=dfs (Tmp.to,min (A,TMP.C-TMP.F)) >0){ $tmp.f+=F; -e[g[x][i]^1].f-=F; -flow+=F; -a-=F; A                 if(a==0) Break; +             } the         } -         if(!flow) d[x]=-1; $         returnflow; the     } the     intMfintSintt) {The function used in the main function to find the maximum flow of s to T the          This->s=s; the          This->t=T; -         intflow=0; in          while(BFS ()) { thememset (cur,0,sizeof(cur)); theflow+=DFS (s,inf); About         } the         returnflow; the     } the }; +     

Network Flow--Maximum flow dinic template

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.