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