Network flow Presumably we all know, in this not too much to repeat. One of the problems in network flow is that you ask for the maximum flow, and for this problem, many computer-based computers give many different algorithms, here-as the title says-we only introduce one of them-\ (\tt{dinic}\)
Dinic is a better algorithm in the maximum flow algorithm, its thought inherits the (ford-fulkerson\) algorithm, but has a great improvement to the FF algorithm. Dinic greatly improves the efficiency of the algorithm through hierarchical graphs, and reduces many unnecessary searches.
Examples
Luogu P3376 "template" Network Max stream
As the title says, the board question
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <queue>using namespaceStdstructzzz{intT,len,nex;} e[100010<<2];inthead[10010],tot=1;voidAddintXintYintz) {E[++tot].t=y;e[tot].len=z;e[tot].nex=head[x];head[x]=tot;}intvis[10010],s,t;//Run through the hierarchy before each searchBOOLBFS () {Queue <int> Q;memset (Vis,0,sizeof(VIS)); Q.push (s); vis[s]=1; while(!q.empty ()) {intK=q.front (); Q.pop (); for(intI=head[k];i;i=e[i].nex) {intto=e[i].t;if(!vis[to]&&e[i].len) {Q.push (to); VIS[TO]=VIS[K]+1;if(to==t)return 1; } }}returnVIS[T];}//Search for augmented pathsintDfsintFromintFlow) {if(From==t| |! FlowreturnFlowintrest=0, FL; for(intI=head[from];i;i=e[i].nex) {intto=e[i].t;if(Vis[to]==vis[from]+1&& (Fl=dfs (To,min (Flow-rest,e[i].len))) {E[I].LEN-=FL; e[i^1].LEN+=FL; REST+=FL;if(Rest==flow)returnFlow }}if(Rest<flow) vis[from]=0;returnRest;}//dinicintDinic () {intans=0; while(BFS ())) Ans+=dfs (S,0x7ffffff);returnAns;}inline intRead () {intk=0;CharC=getchar (); for(;c<' 0 '|| C>' 9 ';) C=getchar (); for(; c>=' 0 '&&c<=' 9 '; C=getchar ()) k= (k<<3) + (k<<1) +c-48;returnK;}intMain () {intN=read (), M=read (); S=read (), T=read (); for(intI=1; i<=m;i++) {intX=read (), Y=read (), Z=read (); Add (x, y, z); Add (Y,x,0);} printf"%d", Dinic ());return 0;}
$\mathfrak{dinic}$ Algorithm for network flow