A very nice way to write MaxFlow via recursion
1 #include <cstdio>
2 #include <algorithm>
3 #include <vector>
4 using namespace std;
5 typedef vector<int> vi;
6 int N, f[300][300], c[300][300], vis[300];
7 vi conn[300];
8
9 int inc(int s, int t, int r) {
10 int rr = 0, x;
11 if (s == t) return r;
12 vis[s] = true;
13 for (vi::iterator it = conn[s].begin(); !rr && it != conn[s].end(); ++it)
14 if ((x = min(c[s][*it]-f[s][*it], r)) && !vis[*it] && (rr=inc(*it, t, x)))
15 f[s][*it] += rr, f[*it][s] -= rr;
16 return rr;
17 }
18
19 int max_flow(int s, int t) {
20 int tot = 0, add = 0;
21 memset(f, 0, sizeof(f));
22 do memset(vis, 0, sizeof(vis)), add = inc(s, t, 1<<28), tot += add;
23 while (add);
24 return tot;
25 }
26
2 #include <algorithm>
3 #include <vector>
4 using namespace std;
5 typedef vector<int> vi;
6 int N, f[300][300], c[300][300], vis[300];
7 vi conn[300];
8
9 int inc(int s, int t, int r) {
10 int rr = 0, x;
11 if (s == t) return r;
12 vis[s] = true;
13 for (vi::iterator it = conn[s].begin(); !rr && it != conn[s].end(); ++it)
14 if ((x = min(c[s][*it]-f[s][*it], r)) && !vis[*it] && (rr=inc(*it, t, x)))
15 f[s][*it] += rr, f[*it][s] -= rr;
16 return rr;
17 }
18
19 int max_flow(int s, int t) {
20 int tot = 0, add = 0;
21 memset(f, 0, sizeof(f));
22 do memset(vis, 0, sizeof(vis)), add = inc(s, t, 1<<28), tot += add;
23 while (add);
24 return tot;
25 }
26