思路:
最大流模板。
实现:
1. Edmonds-Karp,BFS寻找増广路径。
1 #include <iostream> 2 #include <cstdio> 3 #include <queue> 4 #include <cstring> 5 using namespace std; 6 7 const int INF = 0x3f3f3f3f; 8 int m, n, x, y, c; 9 int G[205][205], pre[205]; 10 bool vis[205]; 11 12 int augment() 13 { 14 queue<int> q; 15 q.push(1); 16 memset(pre, 0, sizeof(pre)); 17 memset(vis, 0, sizeof(vis)); 18 vis[1] = true; 19 bool flag = false; 20 while (!q.empty()) 21 { 22 int tmp = q.front(); 23 q.pop(); 24 for (int i = 1; i <= n; i++) 25 { 26 if (G[tmp][i] && !vis[i]) 27 { 28 pre[i] = tmp; 29 vis[i] = true; 30 if (i == n) 31 { 32 flag = true; 33 while (!q.empty()) 34 { 35 q.pop(); 36 } 37 break; 38 } 39 else 40 q.push(i); 41 } 42 } 43 } 44 if (!flag) 45 { 46 return 0; 47 } 48 int now = n; 49 int minn = INF; 50 while (pre[now]) 51 { 52 minn = min(minn, G[pre[now]][now]); 53 now = pre[now]; 54 } 55 now = n; 56 while (pre[now]) 57 { 58 G[pre[now]][now] -= minn; 59 G[now][pre[now]] += minn; 60 now = pre[now]; 61 } 62 return minn; 63 } 64 int main() 65 { 66 while (cin >> m >> n) 67 { 68 memset(G, 0, sizeof(G)); 69 for (int i = 0; i < m; i++) 70 { 71 cin >> x >> y >> c; 72 G[x][y] += c; 73 } 74 int ans = 0; 75 int res = augment(); 76 while (res) 77 { 78 ans += res; 79 res = augment(); 80 } 81 cout << ans << endl; 82 } 83 return 0; 84 }
2. Dinic:先BFS分层,再DFS同时寻找多条増广路径,提高效率。
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <queue> 5 using namespace std; 6 7 const int INF = 0x3f3f3f3f; 8 int G[205][205]; 9 int layer[205], n, m, a, b, l; 10 bool vis[205]; 11 12 bool countLayer() 13 { 14 layer[1] = 0; 15 queue<int> q; 16 q.push(1); 17 memset(vis, 0, sizeof(vis)); 18 vis[1] = true; 19 while (!q.empty()) 20 { 21 int tmp = q.front(); 22 q.pop(); 23 for (int i = 1; i <= n; i++) 24 { 25 if (G[tmp][i] && !vis[i]) 26 { 27 layer[i] = layer[tmp] + 1; 28 if (i == n) 29 { 30 return true; 31 } 32 vis[i] = true; 33 q.push(i); 34 } 35 } 36 } 37 return false; 38 } 39 40 int dinic() 41 { 42 int flow = 0; 43 deque<int> q; 44 while (countLayer()) 45 { 46 memset(vis, 0, sizeof(vis)); 47 vis[1] = true; 48 q.push_back(1); 49 while (!q.empty()) 50 { 51 int tmp = q.back(); 52 if (tmp == n) 53 { 54 int minn = INF; 55 int min_index; 56 for (int i = 1; i < q.size(); i++) 57 { 58 if (G[q[i - 1]][q[i]] && G[q[i - 1]][q[i]] < minn) 59 { 60 minn = G[q[i - 1]][q[i]]; 61 min_index = i - 1; 62 } 63 } 64 for (int i = 1; i < q.size(); i++) 65 { 66 G[q[i - 1]][q[i]] -= minn; 67 G[q[i]][q[i - 1]] += minn; 68 } 69 while (q.size() && q.back() != min_index) 70 { 71 vis[q.back()] = false; 72 q.pop_back(); 73 } 74 flow += minn; 75 } 76 else 77 { 78 bool flag = false; 79 for (int i = 1; i <= n; i++) 80 { 81 if (G[tmp][i] && !vis[i] && layer[i] == layer[tmp] + 1) 82 { 83 vis[i] = true; 84 q.push_back(i); 85 flag = true; 86 break; 87 } 88 } 89 if (!flag && q.size()) 90 { 91 q.pop_back(); 92 } 93 } 94 } 95 } 96 return flow; 97 } 98 99 int main() 100 { 101 while (cin >> m >> n) 102 { 103 memset(G, 0, sizeof(G)); 104 for (int i = 0; i < m; i++) 105 { 106 cin >> a >> b >> l; 107 G[a][b] += l; 108 } 109 cout << dinic() << endl; 110 } 111 return 0; 112 }