Maximum stream dinci Template

Source: Internet
Author: User

We know that the time responsibility of the augmented circuit EK algorithm is O (n * m ^ 2), and the time complexity of finding the shortest augmented path is O (M * n ), therefore, the time complexity mainly lies in finding the augmented path.

Here we introduce another dinci algorithm, which uses BFs to construct a hierarchy chart and then DFS to expand.

Template

# Include <cstdio> # include <cstring> # include <iostream> # include <string> # include <algorithm> # include <vector> # include <queue> using namespace STD; # define del (a, B) memset (a, B, sizeof (A) const int n = 1000; const int INF = 0x3f3f3f; struct edge {int from,, cap, flow ;}; struct dinic {int n, m, S, T; vector <edge> edges; vector <int> G [N]; bool vis [N]; int d [N], cur [N]; void Init (int n) {This-> N = N; For (INT I = 0; I <= N; I ++) g [I]. clear (); edges. clear ();} void addedge (int from, int to, int cap) {// edge creation edges. push_back (edge) {from, to, Cap, 0}); edges. push_back (edge) {to, from, 0, 0}); M = edges. size (); G [from]. push_back (m-2); G [to]. push_back (m-1);} bool BFS () {del (VIS, 0); queue <int> q; q. push (s); D [s] = 0; vis [s] = 1; while (! Q. empty () {int x = Q. front (); q. pop (); For (INT I = 0; I <G [X]. size (); I ++) {edge & E = edges [G [x] [I]; If (! Vis [E. to] & E. cap> E. flow) {vis [E. to] = 1; d [E. to] = d [x] + 1; q. push (E. to) ;}}} return vis [T];} int DFS (int x, int A) {If (x = T | A = 0) return; int flow = 0, F; For (Int & I = cur [X]; I <G [X]. size (); I ++) {edge & E = edges [G [x] [I]; If (d [x] + 1 = d [E. to] & (F = DFS (E. to, min (A, E. cap-e.flow)> 0) {e. flow + = f; edges [G [x] [I] ^ 1]. flow-= f; flow + = f; A-= f; if (a = 0) break;} return flow;} int max_flow (int s, int t) {This-> S = s; this-> T = T; int flow = 0; while (BFS () {del (cur, 0 ); flow + = DFS (S, INF);} return flow ;}}; dinic solve; int main () {int n, m; while (~ Scanf ("% d", & M, & N) {solve. init (n); For (INT I = 0; I <m; I ++) {int x, y, z; scanf ("% d ", & X, & Y, & Z); solve. addedge (x, y, z);} printf ("% d \ n", solve. max_flow (1, N); // start point and end point} return 0 ;}


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.