C++ Code:(ISAP)
#include <cstdio> #include <cstring> #define maxn 1210 #define maxm 120010 int n, m, st, ed; const int inf = 0x7fffffff; inline int min(int a, int b) {return a < b ? a : b;} namespace Network_Flow { int st, ed, MF, n; int head[maxn], cnt = 2; struct Edge { int to, nxt, w; } e[maxm << 1]; inline void addE(int a, int b, int c) { e[cnt] = (Edge) {b, head[a], c}; head[a] = cnt; e[cnt ^ 1] = (Edge) {a, head[b], 0}; head[b] = cnt ^ 1; cnt += 2; } int GAP[maxn], d[maxn]; int last[maxn]; int q[maxn], h, t; inline void init() { GAP[d[ed] = 1] = 1; for (int i = 1; i <= n; i++) last[i] = head[i]; q[h = t = 0] = ed; while (h <= t) { int u = q[h++]; for (int i = head[u]; i; i = e[i].nxt) { int v = e[i].to; if (!d[v]) { d[v] = d[u] + 1; GAP[d[v]]++; q[++t] = v; } } } } int dfs(int u, int low) { if (!low || u == ed) return low; int w, res = 0; for (int &i = last[u]; i; i = e[i].nxt) { int v = e[i].to; if (d[u] == d[v] + 1) { w = dfs(v, min(low, e[i].w)); res += w, low -= w; e[i].w -= w, e[i ^ 1].w += w; if (!low) return res; } } if (!(--GAP[d[u]])) d[st] = n + 1; ++GAP[++d[u]], last[u] = head[u]; return res; } inline void ISAP(int S, int T) { st = S, ed = T; init(); while (d[st] <= n) MF += dfs(st, inf); } inline void clear() { memset(head, 0, sizeof head); cnt = 2; memset(GAP, 0, sizeof GAP); memset(d, 0, sizeof d); MF = 0; } } #define read() R::READ() #include <cctype> namespace R { int x; #ifdef ONLINE_JUGER #define M 1 << 25 char op[M], *ch; inline void init() { fread(ch = op, 1, M, stdin); } inline int READ() { while (isspace(*ch)) ch++; for (x = *ch & 15, ch++; isdigit(*ch); ch++) x = x * 10 + (*ch & 15); return x; } #undef M #else char ch; inline int READ() { ch = getchar(); while (isspace(ch)) ch = getchar(); for (x = ch & 15, ch = getchar(); isdigit(ch); ch = getchar()) x = x * 10 + (ch & 15); return x; } #endif } int main() { #ifdef ONLINE_JUGER R::init(); #endif n = read(), m = read(), st = read(), ed = read(); Network_Flow::n = n; for (int i = 0, a, b, c; i < m; i++) { a = read(), b = read(), c = read(); Network_Flow::addE(a, b, c); } Network_Flow::ISAP(st, ed); printf("%d ", Network_Flow::MF); return 0; }