# 全局最小割 poj2914 Minimum Cut

2017年08月09日 9点热度 0人点赞 0条评论
Minimum Cut
 Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 10046 Accepted: 4190 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 integersAB and C (0 ≤ AB < NA ≠ BC > 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

#include
#include
#include
#define MAXN 505
#define INF 1000000000
using namespace std;
int map[MAXN][MAXN];
int v[MAXN], dis[MAXN];
bool vis[MAXN];
int Stoer_Wagner(int n)
{
int i, j, res = INF;
for(i = 0; i < n; i ++)
v[i] = i;
while(n > 1)
{
int k, pre = 0;
memset(vis, 0, sizeof(vis));
memset(dis, 0, sizeof(dis));
for(i = 1; i < n; i ++)
{
k = -1;
for(j = 1; j < n; j ++)
if(!vis[v[j]])
{
dis[v[j]] += map[v[pre]][v[j]];
if(k == -1 || dis[v[k]] < dis[v[j]])
k = j;
}
vis[v[k]] = true;
if(i == n - 1)
{
res = min(res, dis[v[k]]);
for(j = 0; j < n; j ++)
{
map[v[pre]][v[j]] += map[v[j]][v[k]];
map[v[j]][v[pre]] += map[v[j]][v[k]];
}
v[k] = v[-- n];
}
pre = k;
}
}
return res;
}

int main(){
int n, m, u, v, w;
while(scanf("%d%d", &n, &m) != EOF)
{
memset(map, 0, sizeof(map));
while(m --)
{
scanf("%d%d%d", &u, &v, &w);
map[u][v] += w;
map[v][u] += w;
}
printf("%dn", Stoer_Wagner(n));
}
return 0;
} update