Spoj 4110 Fast Maximum flow (max flow template)

Source: Internet
Author: User

Main topic:

No direction graph, to find the maximum flow.

Algorithm discussion:

Dinic can be too. At last my constant is still too big. Pay attention to it later.

1#include <cstdio>2#include <cstring>3#include <cstdlib>4#include <algorithm>5#include <iostream>6#include <queue>7 using namespacestd;8typedefLong Longll;9  Ten structmf{ One   Static Const intN = the+5; A   Static Const intM =60000+5; -   Static Constll oo =10000000000000LL; -      the   intN, m, s, T, tot, Tim; -   intFirst[n], next[m]; -   intU[m], v[m], cur[n], vi[n]; - ll Cap[m], flow[m], dis[n]; +   intQue[n +N]; -    +   voidClear () { Atot =0; Tim =0; at         for(inti =1; I <= N; + + i) first[i] =-1; -   } -   voidADD (int  from,intto, LL CP, LL FLW) { -U[tot] = from; V[tot] = to; Cap[tot] = CP; Flow[tot] =FLW; -Next[tot] =First[u[tot]]; -First[u[tot]] =tot; in++tot; -   } to   BOOLBFs () { +++Tim; -Dis[s] =0; Vi[s] =Tim; the      *     inthead, tail; $Head = Tail =1;Panax NotoginsengQue[head] =s; -      while(Head <=tail) { the          for(inti = First[que[head]]; I! =-1; i =Next[i]) { +             if(Vi[v[i]]! = Tim && Cap[i] >Flow[i]) { AVi[v[i]] =Tim; theDis[v[i]] = Dis[que[head]] +1; +que[++ tail] =V[i]; -                 } $             } $++head; -         } -        returnVi[t] = =Tim; the   }     -ll Dfs (intX, ll a) {Wuyi     if(x = = T | | a = =0)returnA; thell FLW =0, F; -     int&i =Cur[x]; Wu      for(i = first[x]; I! =-1; i =Next[i]) { -       if(Dis[x] +1= = Dis[v[i]] && (f = DFS (V[i], Min (A, cap[i]-flow[i])) >0){ AboutFlow[i] + = f; flow[i^1] -=F; $A-= f; FLW + =F; -           if(A = =0) Break; -         } -       } A     returnFLW; +   } thell Maxflow (intSintt) { -      This->s = s; This->t =T; $ll FLW =0; the      while(BFS ()) { the        for(inti =1; I <= N; + + i) cur[i] =0; theFLW + =Dfs (S, oo); the     } -     returnFLW; in   } the }net; the intN, M; About   the intMain () { the     intx, y; the ll Z; +scanf"%d%d", &n, &m); -NET.N =N; the net.clear ();Bayi      for(inti =1; I <= m; ++i) { thescanf"%d%d%lld", &x, &y, &z); theNet.add (x, Y, Z,0); -Net.add (y, x, Z,0); -     }     theprintf"%lld\n", Net.maxflow (1, NET.N)); the     return 0; the}
Spoj 4110

Spoj 4110 Fast Maximum flow (max flow template)

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.