题目抽象:n个顶点,m条容量为ci的边组成的图,求源点为1,汇点为n的最大流。
分析:各种最大流算法。
1.ford()
1 #include <cstdio> 2 #include <algorithm> 3 #include <cmath> 4 #include <cstring> 5 using namespace std; 6 const int INF = 0x5fffffff; 7 const int MS = 205; 8 9 struct edge { 10 int c, f; 11 }; 12 13 edge edges[MS][MS]; 14 int flag[MS]; 15 int pre[MS]; 16 int alpha[MS]; 17 int n, m, s, t; 18 int que[MS]; // QUEUE 19 20 int ford() { 21 while (1) { 22 memset(flag, -1, sizeof(flag)); 23 memset(pre, -1, sizeof(pre)); 24 memset(alpha, -1, sizeof(alpha)); 25 flag[1] = 0; 26 pre[1] = 0; 27 alpha[1] = INF; 28 s = t = 0; 29 que[t++] = 1; 30 while (s < t && flag[n] == -1) { 31 int u = que[s++]; 32 for (int v = 1; v <= n; v++) { 33 if (flag[v] == -1) { 34 if (edges[u][v].c > 0 && edges[u][v].f < edges[u][v].c) { 35 flag[v] = 0; 36 pre[v] = u; 37 alpha[v] = min(alpha[u], edges[u][v].c - edges[u][v].f); 38 que[t++] = v; 39 } 40 else if (edges[v][u].c > 0 && edges[v][u].f > 0) { 41 flag[v] = 0; 42 pre[v] = -u; 43 alpha[v] = min(alpha[u], edges[v][u].f); 44 que[t++] = v; 45 } 46 } 47 } 48 flag[u] = 1; 49 } 50 if (flag[n] == -1 || alpha[n] == 0) 51 break; 52 int a = alpha[n]; 53 int k1 = n; 54 int k2 = abs(pre[k1]); 55 while (1) { 56 if (edges[k2][k1].c > 0) 57 edges[k2][k1].f += a; 58 else 59 edges[k1][k2].f -= a; 60 if (k2 == 1) 61 break; 62 k1 = k2; 63 k2 = abs(pre[k1]); 64 } 65 } 66 int maxFlow = 0; 67 for (int i = 1; i <= n; i++) { 68 for (int j = 1; j <= n; j++) { 69 if (i == 1 && edges[i][j].f > 0) 70 maxFlow += edges[i][j].f; 71 } 72 } 73 return maxFlow; 74 } 75 76 int main() { 77 while (scanf("%d%d", &m, &n) != EOF) { 78 for (int i = 0; i <= n; i++) { 79 for (int j = 0; j <= n; j++) { 80 edges[i][j].c = edges[i][j].f = 0; 81 } 82 } 83 int u, v, c; 84 for (int i = 0; i < m; i++) { 85 scanf("%d%d%d", &u, &v, &c); 86 edges[u][v].c += c; // 如有有重边,+= 87 } 88 int maxFlow = ford(); 89 printf("%d\n", maxFlow); 90 } 91 return 0; 92 }
2.dinic算法。
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 using namespace std; 5 const int INF = 0x5fffffff; 6 const int MS = 205; 7 8 int edges[MS][MS]; 9 int level[MS]; 10 int que[MS]; 11 int n, m, s, t; 12 13 int BFS() { 14 memset(level, -1, sizeof(level)); 15 level[1] = 0; 16 s = t = 0; 17 que[t++] = 1; 18 while (s < t) { 19 int u = que[s++]; 20 for (int v = 1; v <= n; v++) { 21 if (level[v] < 0 && edges[u][v] > 0) { 22 level[v] = level[u] + 1; 23 que[t++] = v; 24 } 25 } 26 } 27 if (level[n] > 0) 28 return 1; 29 else 30 return 0; 31 } 32 33 int DFS(int u, int minv) { 34 if (u == n) 35 return minv; 36 int t; 37 for (int v = 1; v <= n; v++) { 38 if (edges[u][v] > 0 && level[v] == level[u] + 1 && (t = DFS(v, min(minv, edges[u][v])))) { 39 edges[u][v] -= t; 40 edges[v][u] += t; 41 return t; 42 } 43 } 44 return 0; 45 } 46 47 int main() { 48 while (scanf("%d%d", &m, &n) != EOF) { 49 memset(edges, 0, sizeof(edges)); 50 int u, v, c; 51 for (int i = 0; i < m; i++) { 52 scanf("%d%d%d", &u, &v, &c); 53 edges[u][v] += c; 54 } 55 int ans = 0; 56 int t; 57 while (BFS()) { 58 while ((t = DFS(1, INF))) 59 ans += t; 60 } 61 printf("%d\n", ans); 62 } 63 return 0; 64 }
时间: 2024-10-11 21:48:23