Front-facing knowledge network maximum inflow in front of the word
Dinic is one of the most commonly used algorithms for finding the maximum flow of a network in the Informatics Olympiad.
It relies on the idea of intuitive, code difficult, superior performance and other advantages, favored by the vast number of Oier
Thought
$Dinic $ algorithm belongs to the augmented path algorithm.
Its core idea is: For each point , the edge of its augmentation, when augmented, each augmented "maximum flow"
Unlike the EK algorithm, the EK algorithm starts with the edge, and the dinic algorithm starts from the point
At the time of augmentation, we try to augment the edge of a point even out, that is, the multi-channel augmentation
The Dinic algorithm also introduces the concept of a hierarchical graph , that is, for the $i$ node, a $dis (i) $ represents its distance from the source point, and specifies that an edge can be augmented when and only if the two points it is connected to $u,v$ satisfy: $dis (v) =dis (u) +1$, This can greatly optimize their time complexity.
Realize
With the above knowledge, Dinic is more simple to implement.
Each BFS constructs a hierarchical map (note that it must be reconstructed every time, because some useless edges are removed after each augmentation, and some useless points are removed)
And then start the multi-channel augmentation from the source point
Optimization
- Current ARC optimization: For each point, we record which edges it has augmented, and when it returns to this point, ignore the already augmented edges and begin to augment from the next edge.
- Hierarchical optimization (name of your own XJB): Find a meeting point to exit immediately when layering
- Residual quantity optimization (also own): When the augmentation, if the node has no traffic, the direct exit
Complexity of Time
The theoretical time complexity of the dinic algorithm is $o (N^2*M) $
Proof can be seen here
But!
The performance of the Dinic algorithm is superior in the game.
According to training Team Ly, we can assume that the time complexity of the dinic algorithm is linear (it is not known where it is higher than a certain marking algorithm)
Code
Topic links
#include <cstdio>#include<cstring>#include<queue>#defineAddedge (x, Y, z) add_edge (x, Y, z), Add_edge (y,x,0);using namespacestd;Const intmaxn=1e6+1;Const intinf=1e8+Ten; inlineCharNC () {Static CharBuf[maxn],*p1=buf,*p2=buf; returnp1==p2&& (p2= (p1=buf) +fread (buf,1, Maxn,stdin), P1==P2)? eof:*p1++;} InlineintRead () {CharC=NC ();intx=0, f=1; while(c<'0'|| C>'9'){if(c=='-') f=-1; c=NC ();} while(c>='0'&&c<='9') {x=x*Ten+c-'0'; c=NC ();} returnx*F;}intn,m,s,t;structnode{intV,FLOW,NXT;} EDGE[MAXN*4];inthead[maxn],cur[maxn],num=0;//Note that this must start from 0 .InlinevoidAdd_edge (intXintYintz) {EDGE[NUM].V=y; Edge[num].flow=Z; EDGE[NUM].NXT=Head[x]; HEAD[X]=num++;}intDeep[maxn],q[maxn];inlineBOOLBFS () {memset (deep,0,sizeof(deep)); Deep[s]=1; intL=0, r=1; q[++l]=R; while(l<=r) {intp=q[l++]; for(inti=head[p];i!=-1; i=edge[i].nxt)if(!deep[edge[i].v]&&Edge[i].flow) {DEEP[EDGE[I].V]=deep[p]+1; q[++r]=edge[i].v; if(edge[i].v==t)return 1;//when the meeting point is found, return to fast 30ms. } } returndeep[t];}intDFS (intNowintNowflow) { if(now==t)returnNowflow; inttotflow=0;//How much traffic can be augmented from this point for(inti=head[now];i!=-1; i=edge[i].nxt)//Current ARC optimization Fast 150ms { if(deep[edge[i].v]==deep[now]+1&&edge[i].flow)//only points that meet distance requirements and traffic requirements can be augmented { intcanflow=DFS (Edge[i].v,min (Nowflow,edge[i].flow)); Edge[i].flow-=canflow;edge[i^1].flow+=canflow;//augmentedtotflow+=Canflow; Nowflow-=Canflow; if(nowflow<=0) Break;//The current point has no traffic fast 100ms } } returnTotflow;}voidDinic () {intans=0; while(BFS ())//each time you construct a hierarchy chart{memcpy (Cur,head,sizeof(head));//Current ARC OptimizationAns+=dfs (S,inf);//to augment} printf ("%d", ans);}intMain () {#ifdef WIN32 freopen ("a.in","R", stdin); #else #endifN=read (); M=read (); S=read (); t=read (); memset (Head,-1,sizeof(head)); for(intI=1; i<=m;i++) { intx, Y, Z X=read (); Y=read (); z=read (); Addedge (x, y, z); } dinic (); return 0;}
-dinic algorithm and optimization of network maximum flow algorithm