Time Limit: 10000MS | Memory Limit: 65536K | |
Total Submissions: 9913 | Accepted: 4158 | |
Case Time Limit: 5000MS |
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 M integers 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模板题,找出一个图中的最小割使得整个图分成2个联通块
#include<cstdio>
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std;
const int MX = 505;
const int inf = 0x3f3f3f3f;
struct Stoer_Wagner {
int mp[MX][MX], s, t;
int v[MX], d[MX];
void init(int n) {
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
mp[i][j] = 0;
}
void add(int u, int v, int w) {
mp[u][v] += w;
mp[v][u] += w;
}
int solve(int n) {
int i, j, now, ret = inf;
for (i = 0; i < n; i++)v[i] = i;//下标从0开始
while (n > 1) {
for (now = 0, i = 1; i < n; i++) d[v[i]] = 0;
for (i = 1; i < n; i++) {
swap(v[now], v[i - 1]);
for (now = j = i; j < n; j++) {
d[v[j]] += mp[v[i - 1]][v[j]];
if (d[v[now]] < d[v[j]])now = j;
}
}
if (ret > d[v[now]]) {
ret = d[v[now]];
s = v[now - 1];
t = v[now];
}
for (j = 0; j < n; j++)
mp[v[j]][v[now - 1]] = mp[v[now - 1]][v[j]] += mp[v[j]][v[now]];
v[now] = v[--n];
}
return ret;
}
} F;