[Integration] ISAP algorithm templates for network streams and isap algorithm templates

Source: Internet
Author: User

[Integration] ISAP algorithm templates for network streams and isap algorithm templates

The comment should be clear =
# Include <iostream> # include <cstdio> # include <cstring> # include <cmath> # include <algorithm> # define MAXN 2048 # define MAXINT 0x7fffffusing namespace std; int n, m; // number of points and edge number int dis [MAXN + 1]; // distance label int num [MAXN + 1]; int src, des; // src Source point, des sink point int u, v, w; struct edge {int f; // int c of the current traffic of a certain arc; // capacity int ver; // Arc Start edge * rev; // arc edge * next; // arc end edge () {}; edge (int v, int cap, edge * nex): ver (v), c (cap), next (nex), rev (NULL), f (0) {}; void * operator new (size_t, void * p) {return p;} * s [MAXN + 1]; void init () {int queue [MAXN + 1], head = 0, tail = 0; for (int I = 1; I <= n; I ++) dis [I] = MAXN, num [I] = 0; queue [tail ++] = des; dis [des] = 0; num [0] = 1; while (head! = Tail) {int v = queue [head ++]; edge * e = s [v]; while (e) {if (e-> rev) & (e-> rev-> c = 0) | dis [e-> ver] <MAXN) {} // If the modified edge cannot pass through the stream or has been updated, you do not need to update else {dis [e-> ver] = dis [v] + 1; ++ num [dis [e-> ver]; queue [tail ++] = e-> ver;} e = e-> next ;}} int maxflow () // ISAP algorithm implementation process {int st = src, ret = 0; edge * E [MAXN], * rep [MAXN]; for (int I = 1; I <= n; I ++) E [I] = s [I]; while (dis [src] <n) // if the augmented path does not exist, stop {if (st = des) // find the augmented path {int delta = MAXINT; for (int I = src; I! = Des; I = E [I]-> ver) delta = min (delta, E [I]-> c); for (int I = src; I! = Des; I = E [I]-> ver) {E [I]-> c-= delta; E [I]-> f + = delta; E [I]-> rev-> c + = delta; E [I]-> rev-> f-= delta;} ret + = delta; st = src ;} edge * e; for (e = E [st]; e = e-> next) if (e-> c> 0 & dis [st] = dis [e-> ver] + 1) break; if (e) // The Arc {E [st] = e; rep [e-> ver] = e-> rev; st = e-> ver;} is allowed ;} else {if (-- num [dis [st]) = 0) break; // GAP optimization E [st] = s [st]; int mind = n; for (edge * t = s [st]; t = t-> next) if (t-> c> 0) mind = min (mind, dis [t-> ver]); dis [st] = mind + 1; ++ num [dis [st] ]; If (st! = Src) st = rep [st]-> ver ;}} return ret ;}int main () {freopen ("ditch. in "," r ", stdin); freopen (" ditch. out "," w ", stdout); scanf (" % d ", & m, & n); src = 1, des = n; edge * buffer = new edge [2 * m]; edge * data = buffer; while (m --) {scanf ("% d", & u, & v, & w); s [u] = new (void *) data ++) edge (v, w, s [u]); s [v] = new (void *) data ++) edge (u, 0, s [v]); s [u]-> rev = s [v]; s [v]-> rev = s [u];} init (); printf ("% d", maxflow ());}

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.