Minimum Cut
Description Given an undirected graph, in which two vertices can be connected by multiple edges, what is the size of the minimum cut of the graph? i.e. how many edges must be removed at least to disconnect the graph into two subgraphs? Input Input contains multiple test cases. Each test case starts with two integers N and M (2 ≤ N ≤ 500, 0 ≤ M ≤ N × (N − 1) ⁄ 2) in one line, where N is the number of vertices. Following are M lines, each line contains Mintegers A, B and C (0 ≤ A, B < N, A ≠ B, C > 0), meaning that there C edges connecting vertices A and B. Output There is only one line for each test case, which contains the size of the minimum cut of the graph. If the graph is disconnected, print 0. Sample Input 3 3 0 1 1 1 2 1 2 0 1 4 3 0 1 1 1 2 1 2 3 1 8 14 0 1 1 0 2 1 0 3 1 1 2 1 1 3 1 2 3 1 4 5 1 4 6 1 4 7 1 5 6 1 5 7 1 6 7 1 4 0 1 7 3 1 Sample Output 2 1 2 Source |
[Submit] [Go Back] [Status] [Discuss]
无向图的边连通度,有一个神奇的算法,还有我蒟蒻的模板。
1 #include <cstdio> 2 #include <cstring> 3 4 inline int min(int a, int b) 5 { 6 return a < b ? a : b; 7 } 8 9 const int inf = 2e9; 10 const int maxn = 505; 11 12 int n, m; 13 int vis[maxn]; 14 int wet[maxn]; 15 int com[maxn]; 16 int G[maxn][maxn]; 17 18 inline int search(int &S, int &T) 19 { 20 memset(vis, 0, sizeof(vis)); 21 memset(wet, 0, sizeof(wet)); 22 23 S = -1, T = -1; 24 25 int id, maxi, ret = 0; 26 27 for (int i = 0; i < n; ++i) 28 { 29 maxi = -inf; 30 31 for (int j = 0; j < n; ++j) 32 if (!com[j] && !vis[j] && wet[j] > maxi) 33 id = j, maxi = wet[j]; 34 35 if (id == T) 36 return ret; 37 38 S = T; 39 T = id; 40 ret = maxi; 41 vis[id] = 1; 42 43 for (int j = 0; j < n; ++j) 44 if (!com[j] && !vis[j]) 45 wet[j] += G[id][j]; 46 } 47 } 48 49 inline int StoerWagner(void) 50 { 51 int ret = inf, S, T; 52 53 memset(com, 0, sizeof(com)); 54 55 for (int i = 0; i < n - 1; ++i) 56 { 57 ret = min(ret, search(S, T)); 58 59 if (!ret)return 0; 60 61 com[T] = 1; 62 63 for (int j = 0; j < n; ++j) 64 if (!com[j]) 65 G[S][j] += G[T][j], 66 G[j][S] += G[j][T]; 67 } 68 69 return ret; 70 } 71 72 signed main(void) 73 { 74 while (~scanf("%d%d", &n, &m)) 75 { 76 memset(G, 0, sizeof(G)); 77 78 for (int i = 1; i <= m; ++i) 79 { 80 int x, y, w; 81 scanf("%d%d%d", &x, &y, &w); 82 G[x][y] += w; 83 G[y][x] += w; 84 } 85 86 printf("%d ", StoerWagner()); 87 } 88 }
@Author: YouSiki