Dinic algorithm Valley 3376 Los maximum flow test

Topic links: https://www.luogu.com.cn/problem/P3376

Reference blog: https://www.cnblogs.com/SYCstudio/p/7260613.html

Dinic maximum flow algorithm, based on EdmondsKarp algorithms coupled with the concept of hierarchical graph BFS, making augmented more direction, and in a DFS can find a number of augmenting paths, the complexity of the algorithm is O (| E | * | V | ^ 2 times), but the speed of the algorithm is actually more complex than the theory of the time described to be fast. Thought Dinic algorithm is such that, for each point, if he has many a time, then you can branch to each branch to conduct a search for an augmenting path, rather than from the point to start from scratch src augmented. Every time a dfs will return to the current point after the branch, the branch can now be spent to update the size of this flow, the next available branch flow will be reduced.

code show as below:

  1 #include<bits/stdc++.h>
  2 using namespace std;
  3 typedef unsigned int ui;
  4 typedef long long ll;
  5 typedef unsigned long long ull;
  6 #define pf printf
  7 #define mem(a,b) memset(a,b,sizeof(a))
  8 #define prime1 1e9+7
  9 #define prime2 1e9+9
 10 #define pi 3.14159265
 11 #define lson l,mid,rt<<1
 12 #define rson mid+1,r,rt<<1|1
 13 #define scand(x) scanf("%llf",&x) 
 14 #define f(i,a,b) for(int i=a;i<=b;i++)
 15 #define scan(a) scanf("%d",&a)
 16 #define mp(a,b) make_pair((a),(b))
 17 #define P pair<int,int>
 18 #define dbg(args) cout<<#args<<":"<<args<<endl;
 19 #define inf 0x7ffffff
 20 inline int read(){
 21     int ans=0,w=1;
 22     char ch=getchar();
 23     while(!isdigit(ch)){if(ch=='-')w=-1;ch=getchar();}
 24     while(isdigit(ch))ans=(ans<<3)+(ans<<1)+ch-'0',ch=getchar();
 25     return ans*w;
 26 }
 27  
 28 const int maxn=1e5+10;
 29 int n,m,s,t;
 30 int cnt=0;
 31 int head[maxn],nxt[maxn*10],d[maxn];
 32 struct node{
 33     int v,w;
 34 }p[maxn*10];
 35 int e;
 36 void addedge(int u,int v,int w)
 37 {
 38     p[e].v=v;
 39     p[e].w=w;
 40     nxt[e]=head[u];
 41     head[u]=e++;
 42 }
 43 bool bfs(int src,int sink)//确定bfs序 
 44 {
 45      MEM (D, 0 );
 46 is      D [the src] = . 1 ;
 47      Queue < int > Q;
 48      q.push (the src);
 49      the while (! Q.empty ())
 50      {
 51 is          int CUR = Q. Front ();
 52 is          q.pop ();
 53 is          for ( int I = head [CUR]; ~ I; I = NXT [I])
 54 is          {
 55              int V = P [I] .v;
 56 is              IF (D! [V] && P [I] .W) // determine the reference point is not, and is not back edges 
57             {
 58                 d[v]=d[cur]+1;
 59                 q.push(v);
 60             }
 61         }
 62     }
 63     if(d[sink])return true;
 64     return false;
 65 }
 66 int dfs(int s,int flow)
 67 {
 68     if(s==t)return flow;
 69     int used=0;
 70     for(int I = head [S]; ~ I; I = NXT [I])
 71 is      {
 72          int V = P [I] .v, W = P [I] .W;
 73 is          IF (D [V] == D [S] + . 1 && W> 0 ) // according to the idea Dinic algorithm, can only go forward, BFS order freshman edge 
74          {
 75              int tmp = DFS (V, min (flow- Used, W));
 76              IF (tmp> 0 )
 77              {
 78                  P [I] .w- = tmp; // updates the forward flow and the reverse flow side edges, 
79                  P [I ^ . 1 ] .W + = tmp; // n the edge is even, its corresponding reverse side is the forward side +1 
80                 + = tmp Used; // flow from a starting point is the most flow, the flow rate needs to be updated spent 
81                  IF (Used == Flow) BREAK ; 
 82               } 
 83          }
 84      }
 85      return Used;
 86  }
 87  int Dinic ()
 88  {
 89      int ANS = 0 ;
 90      the while (BFS (S, T))
 91 is      {
 92          ANS + = DFS (S, INF);
 93      }
 94      return ANS;
 95  }
 96 int main()
 97 {
 98     //freopen("input.txt","r",stdin);
 99     //freopen("output.txt","w",stdout);
100     std::ios::sync_with_stdio(false);
101     n=read(),m=read(),s=read(),t=read();
102     mem(head,-1);mem(nxt,-1);
103     int u,v,w;
104     f(i,1,m)
105     {
106         u=read(),v=read(),w=read();
107         addedge(u,v,w);addedge(v,u,0);
108     }
109     int maxflow=dinic();
110     pf("%d\n",maxflow);
111 } 

 

Guess you like

Origin www.cnblogs.com/randy-lo/p/12591689.html