最大流
Dinic
1 struct Edge { 2 int to, f, c; 3 }; 4 5 struct Dinic { 6 int n, m, s, t; 7 vector<Edge> edges; 8 vector<int> G[N]; 9 bool vis[N]; 10 int d[N], cur[N]; 11 12 void AddEdge (int from, int to, int cap) { 13 G[from].push_back(edges.size()); 14 edges.push_back((Edge){to, 0, cap}); 15 } 16 17 bool BFS() { //make a depth map 18 memset(vis,0,sizeof vis); 19 queue<int> que; 20 que.push(s); 21 d[s] = 0, vis[s] = 1; 22 while (!que.empty()) { 23 int u = que.front(); 24 que.pop(); 25 int sz = G[u].size(); 26 for (int i = 0; i < sz; ++i) { 27 Edge &e = edges[G[u][i]]; 28 if (!vis[e.to] && e.c > e.f) { 29 vis[e.to] = 1; 30 d[e.to] = d[u] + 1; 31 que.push(e.to); 32 } 33 } 34 } 35 return vis[t]; 36 } 37 38 int DFS(int u, int a) { 39 if (u == t || a == 0) return a; 40 int flow = 0, f; 41 int sz = G[u].size(); 42 for (int &i = cur[u]; i < sz; ++i) { 43 Edge &e = edges[G[u][i]]; 44 if (d[u] + 1 == d[e.to] && (f = DFS(e.to, min(a, e.c - e.f))) > 0) { 45 e.f += f; 46 edges[G[u][i]^1].f -= f; 47 flow += f; 48 a -= f; 49 if (a==0) break; 50 } 51 } 52 return flow; 53 } 54 55 int Maxflow() { 56 int flow = 0; 57 while (BFS()) { 58 memset(cur, 0, sizeof cur); 59 flow += DFS(s, INF); 60 } 61 return flow; 62 } 63 };
ISAP
1 struct Edge { 2 int from, to, f, c; 3 }; 4 5 struct ISAP { 6 int n, m, s, t; 7 int p[N], num[N], d[N], cur[N]; 8 bool vis[N]; 9 vector<int> G[N]; 10 vector<Edge> edges; 11 12 void AddEdge (int from, int to, int cap) { 13 edges.push_back((Edge){from, to, 0, cap}); 14 edges.push_back((Edge){to, from, 0, 0}); 15 int s = edges.size(); 16 G[from].push_back(s-2); 17 G[to].push_back(s-1); 18 } 19 20 bool BFS() { 21 memset(vis, 0, sizeof vis); 22 queue<int> que; 23 for (int i = 1; i <= n; ++i) d[i] = n; 24 d[t] = 0, vis[t] = 1; 25 que.push(t); 26 while (!que.empty()) { 27 int u = que.front(); 28 que.pop(); 29 int sz = G[u].size(); 30 for (int i = 0; i < sz; ++i) { 31 Edge &e = edges[G[u][i]]; 32 if (!vis[e.to] && edges[G[u][i]^1].c > edges[G[u][i] ^ 1].f) { 33 d[e.to] = d[u] + 1; 34 vis[e.to] = 1; 35 que.push(e.to); 36 } 37 } 38 } 39 return vis[s]; 40 } 41 42 int Augment() { 43 int x = t, a = INF; 44 while (x!=s) { 45 Edge &e = edges[p[x]]; 46 a = min(a, e.c-e.f); 47 x = e.from; 48 } 49 x = t; 50 while (x != s) { 51 edges[p[x]].f += a; 52 edges[p[x] ^ 1].f -= a; 53 x = edges[p[x]].from; 54 } 55 return a; 56 } 57 58 int Maxflow() { 59 int flow = 0, x = s; 60 BFS(); 61 memset(cur, 0, sizeof cur); 62 memset(num, 0, sizeof num); 63 for (int i = 1; i <= n; ++i) ++num[d[i]]; 64 while (d[s] < n) { 65 if (x == t) 66 flow += Augment(), x = s; 67 bool ok = 0; 68 int sz = G[x].size(); 69 for (int i = cur[x]; i < sz; ++i) { 70 Edge &e = edges[G[x][i]]; 71 if (e.c > e.f && d[x] == d[e.to] + 1) { 72 ok = 1; 73 p[e.to] = G[x][i]; 74 cur[x] = i; 75 x = e.to; 76 break; 77 } 78 } 79 if (!ok) { 80 int m = n-1, sz=G[x].size(); 81 for (int i = 0; i < sz; ++i) { 82 Edge &e = edges[G[x][i]]; 83 if (e.c > e.f) m = min(m, d[e.to]); 84 } 85 if (--num[d[x]] == 0) break; 86 ++num[d[x] = m + 1]; 87 cur[x] = 0; 88 if(x != s) x = edges[p[x]].from; 89 } 90 } 91 return flow; 92 } 93 };