"Algorithm" Network maximum flow Dinic

Source: Internet
Author: User

Dinic's general idea is similar to the EK (in fact, a lot of the general idea of the algorithm is the same), but dinic in each search for augmented road first BFS a bit, to each point plus a grade, and rules: Only the level of two adjacent points can go, So when it comes to DFS, you lose a lot of useless, unnecessary roads.

1#include <algorithm>2#include <iostream>3#include <cstring>4#include <cstdio>5#include <queue>6 using namespacestd;7 Const intinf=1000000000, maxn=100000+Ten, maxm=1000000+Ten;8 intTo[maxm],nex[maxm],cap[maxm],beg[maxn],level[maxn],n,m,s,t,e=1;//To,nex,cap is related to the edge, so the MAXM is fixed, and beg,level is related to the point, so the maxn,level is used for layering, cap and flow two arrays reduced to a cap, indicating the remaining capacity, space saving9InlinevoidReadint&x)Ten { Onex=0; A     CharC=GetChar (); -      while(c<'0'|| C>'9') c=GetChar (); -      while(c>='0'&&c<='9') the     { -X= (x<<3) + (x<<1) + (c^'0'); -C=GetChar (); -     } + } -InlinevoidInsertintXintYintZ//chain-forward star plus edge + { A++e;//the initial value of E is 1, because the following will be used ^ (XOR) atto[e]=y; -nex[e]=Beg[x]; -beg[x]=e; -cap[e]=Z; -++e; -to[e]=x; innex[e]=Beg[y]; -beg[y]=e; tocap[e]=0; +}//The network flow is a forward graph, but there is a reverse arc, so build two, but the inverse of the CAP value is initialized to 0, because the forward no traffic, the reverse is not flow -InlineBOOLBFS ()//BFS tiering the { *Memset (Level,0,sizeof(level));//level is initialized to 0 $level[s]=1;//The starting level is one, so if there is no augmented path to the end, return falsePanax Notoginsengqueue<int> q;//defining BFS Standard queues -Q.push (s);//Start the queue the      while(!q.empty ())//There are also elements in the queue that are not accessed +     { A         intX=q.front ();//Remove the first element of the team theQ.pop ();//Delete team first element +          for(RegisterintI=beg[x];i;i=nex[i])//chain forward-star standard cycle -         { $             if(Cap[i]&&!level[to[i]])//There is also capacity remaining and the point has not been accessed $             { -level[to[i]]=level[x]+1;//Assign Class -Q.push (To[i]);//this point joins the queue the             } -         }Wuyi     } the     returnLEVEL[T];//returns True if there is also an augmented path from the start to end, otherwise false - } WuInlineintDfsintXintMaxflow)//The equivalent of an EK path return and add traffic, DFS Implementation, x represents the current position (point), Maxflow represents the maximum value of the remaining capacity before - { About     if(!maxflow| | X==T)returnMaxflow;//If there is no surplus or has reached the end, return directly $     intres=0; -      for(RegisterintI=beg[x];i;i=Nex[i]) -     { -         if(cap[i]&&level[to[i]]==level[x]+1)//There is also capacity, and the point to the original position of the level is exactly 1, that is, connected and feasible A         { +             intF=dfs (To[i],min (maxflow,cap[i));//keep going and compare the remaining capacity with the capacity of this edge to update the remaining capacity, that is, to find bottlenecks theRes+=f;//The biggest flow on this side plus the bottleneck. -Maxflow-=f;//maximum remaining capacity minus bottleneck $Cap[i]-=f;//the edge remaining capacity minus the bottleneck thecap[i^1]+=f;//The edge reverse edge capacity plus bottleneck the         } the     } the     returnRes//return - } inInlineintDinic ()//Dinic to find the maximum flow of network flow the { the     intres=0; About      while(BFS ())) Res+=dfs (S,inf);//res plus the maximum flow of each augmented path from the starting point to the end point the     returnRes; the } the intMain () + { -Read (n); read (m); Read (s); read (t);//S is the starting point, T is the end point, N is the point, and M is the number of sides the      for(RegisterintI=1; i<=m;++i)Bayi     { the         intx, y, z the Read (x); Read ( y); read (z); - Insert (x, y, z); -     } theprintf"%d", Dinic ()); the     return 0; the}
Network Maximum flow dinic

"Algorithm" Network maximum flow Dinic

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.