Maximum Flow Dinic

Source: Internet
Author: User

Continuous construction of layered network with BFS, using DFS augmentation. Halfway through the cur to optimize DFS.

Dinic Package Template:

1#include <cstdio>2#include <iostream>3#include <algorithm>4#include <cstring>5#include <cmath>6#include <queue>7 using namespacestd;8typedefLong LongL;9 Const intMAXN =10000+Ten;Ten Const intMAXM =80000+Ten; One Const intINF =-1u>>1; A intFCH[MAXN], N, m; -L ans =0; - BOOLVIS[MAXN]; the structtedge{ -     int  from, to, cap, flow, next; - }ADJ[MAXM]; - structdinic{ +     intS, T, N, MS; -     intD[MAXN], CUR[MAXN], FCH[MAXN]; +     voidInitintN) { A          This-n =N; atmemset (FCH,-1,sizeof(FCH)); -ms =0; -         return ; -     } -     voidAddedge (intUintVintc) { -ADJ[MS] = (Tedge) {u, V, C,0, Fch[u]}; inFch[u] = ms + +; -ADJ[MS] = (Tedge) {V, U,0,0, Fch[v]}; toFCH[V] = ms + +; +         return ; -     } the     BOOLBFS () { *memset (Vis,0,sizeof(Vis)); $queue<int>Q;Panax NotoginsengQ.push (S); Vis[s] =true; D[s] =0; -          while(!Q.empty ()) { the             intx =Q.front (); Q.pop (); +              for(inti = fch[x]; I! =-1; i =Adj[i].next) { Atedge& e =Adj[i]; the                 if(!vis[e.to] && e.cap >E.flow) { +Vis[e.to] =true; -D[e.to] = D[x] +1; $ Q.push (e.to); $                 } -             } -         } the         returnVis[t]; -     }Wuyi     intDFS (intXinta) { the         if(x = = T | |!a)returnA; -         intFlow =0, F; Wu          for(int& i = cur[x]; I! =-1; i =Adj[i].next) { -tedge& e =Adj[i]; About             if(d[e.to] = = D[x] +1&& (f = DFS (e.to, Min (A, e.cap-e.flow))) >0){ $Flow + =F; -A-=F; -E.flow + =F; -Adj[i ^1].flow-=F; A                 if(!a) Break; +             } the         } -         returnflow; $     } theL Max_flow (intSintT) { the          ThisS =S; the          This-T =T; theL flow =0; -          while(BFS ()) { in              for(inti =0; I < n; i + +) cur[i] =Fch[i]; theFlow + =DFS (S, INF); the         } About         returnflow; the     } the }sol; the voidReadint&x) { +x =0;intsig =1;CharCH =GetChar (); -      while(!isdigit (CH)) {if(ch = ='-') sig =-1; CH =GetChar ();} the      while(IsDigit (ch)) x =Ten* x + CH-'0', ch =GetChar ();BayiX *= sig;return ; the } the voidinit () { - read (n); read (m); - sol.init (n); the     intu, V, W; the      for(inti =0; I < m; i++){ the Read (u); Read ( v); Read (w); the Sol. Addedge (U, V, W); -     } the     return ; the } the voidWork () {94Ans = sol. Max_flow (1, n); the     return ; the } the voidprint () {98printf"%lld\n", ans); About     return ; - }101 intMain () {102 init ();103 Work ();104 print (); the     return 0; 106}

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.