$\mathfrak{dinic}$ Algorithm for network flow

Source: Internet
Author: User

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

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.