Maximum stream dinic + sap Template

Source: Internet
Author: User

If you do not want to mention anything else, go directly to the template.

 

Dinic + current arc optimization:

Struct edge {int X, Y, C, Ne;} e [M * 2]; int be [N], all; int d [N], Q [N]; int stack [N], top; // stack stores edge int cur [N]; // Current Arc Optimization
Void add (int x, int y, int Z) // you must ensure that the first side of the opposite side is an even number {e [all]. X = x; E [all]. y = y; E [all]. C = z; E [all]. ne = be [x]; Be [x] = All ++; E [all]. X = y; E [all]. y = x; E [all]. C = 0; E [all]. ne = be [y]; Be [y] = All ++ ;}
Bool BFS (int s, int t) // The hierarchy graph reduces the number of edges from m to N complexity. {memset (D,-1, sizeof (d )); int head = 0, tail = 0; Q [++ tail] = s; d [s] = 0; while (Head! = Tail) {int u = Q [++ head]; for (INT I = be [u]; I! =-1; I = E [I]. ne) if (E [I]. c> 0 & D [E [I]. y] =-1) {d [E [I]. y] = d [u] + 1; Q [++ tail] = E [I]. y; If (tail = N-1) tail = 0; If (E [I]. y = T) return 1 ;}} return 0 ;}int dinic (int s, int t) // use stack to simulate recursion. {int ans = 0; while (BFS (S, T) {memcpy (cur, Be, sizeof (be); int u = s; Top = 0; // DFS starts clearing the stack while (1) {If (u = T) {int minc = 1000000000, mini; For (INT I = 0; I <top; I ++) if (minc> E [stack [I]. c) {minc = E [stack [I]. c; Mini = I; // to return This continues to be extended} For (INT I = 0; I <top; I ++) {e [stack [I]. c-= minc; E [stack [I] ^ 1]. c + = minc; // The first binary is reversed to obtain the reverse edge.} ans + = minc; Top = mini; u = E [stack [mini]. X ;}for (INT I = cur [u]; I! =-1; cur [u] = I = E [cur [u]. ne) if (E [I]. c> 0 & D [E [I]. y] = d [E [I]. x] + 1) break; If (cur [u]! =-1) {stack [top ++] = cur [u]; u = E [cur [u]. y;} else {If (Top = 0) break; // The End mark of the loop D [u] =-1; // The current node does not delete u = E [stack [-- top] in the augmented path. x; // backtracking }}return ans ;}

ISAP + gap + current arc optimization:

Struct edge {int X, Y, C, Ne;} e [M * 2]; int X, Y, Z, n, m, S, T; int be [N], all; int d [N], Q [N]; int stack [N]; // simulate recursive int gap [N], cur [N]; // gap optimization + current arc optimization void add (int x, int y, int Z) // ensure that the first one is an even number {e [all]. X = x; E [all]. y = y; E [all]. C = z; E [all]. ne = be [x]; Be [x] = All ++; E [all]. X = y; E [all]. y = x; E [all]. C = 0; E [all]. ne = be [y]; Be [y] = All ++;} void BFS (int s, int t) {memset (D,-1, sizeof (d )); memset (GAP, 0, sizeof (GAP); Gap [0] = 1; int head = 0, Ta IL = 0; Q [++ tail] = T; d [T] = 0; while (Head! = Tail) {int u = Q [++ head]; for (INT I = be [u]; I! =-1; I = E [I]. ne) if (d [E [I]. y] =-1) {d [E [I]. y] = d [u] + 1; Q [++ tail] = E [I]. y; Gap [d [E [I]. y] ++ ;}}} int SAP (int s, int T, int N) {int ans = 0; BFS (S, T); memcpy (cur, be, sizeof (be); int Top = 0; int u = s; while (d [s] <n) {If (u = T) {int minc = 1000000000, mini; For (INT I = 0; I <top; I ++) if (minc> E [stack [I]. c) {minc = E [stack [I]. c; Mini = I ;}for (INT I = 0; I <top; I ++) {e [stack [I]. c-= minc; E [stack [I] ^ 1]. c + = minc;} ans + = Minc; Top = mini; u = E [stack [mini]. X; continue;} For (INT I = cur [u]; I! =-1; cur [u] = I = E [I]. ne) // Current Arc optimization if (E [I]. c> 0 & D [E [I]. y] + 1 = d [u]) break; If (cur [u]! =-1) {stack [top ++] = cur [u]; u = E [cur [u]. y;} else {int mind = N; For (INT I = be [u]; I! =-1; I = E [I]. ne) // update the distance label if (E [I]. c> 0 & mind> d [E [I]. y]) {mind = d [E [I]. y]; cur [u] = I;} gap [d [u] --; If (! Gap [d [u]) return ans; // gap indicates the number of points at the current distance. Once = 0, the fault exits directly. d [u] = mind + 1; gap [d [u] ++; If (u! = S) u = E [stack [-- top]. X ;}} return ans;} void Init () {All = 0; memset (be,-1, sizeof (be ));}

 

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.