Pku2914 Minimum Cut
时间限制: 3 Sec内存限制: 512 MB提交: 0解决: 0
提交状态
题目描述
有一个N(<=500)的无向图,求将这个图断成两个联通块需要删除的边的边权和最小值。
输入
两个数n,m,然后m个数每行三个数a,b,c,表示a和b这两个点的连系为c (不会重复出现一对a和b,无序)
输出
一个数表示最小的联系度和(即无向图最小割)
样例输入
3 3
1 2 1
2 3 1
3 1 2
样例输出
2
简单暴力的最小割集求解
stoer-wagner算法求解
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
#define N 505
#define INF 1<<30
int vist[N],wet[N],map[N][N],conbine[N];
int s,t,mincut,n,m;
void search()
{
int i, j, Max, tmp;
memset(vist, 0, sizeof(vist));
memset(wet, 0, sizeof(wet));
s = t = -1;
for (i = 0; i < n; i++)
{
Max = -INF;
for (j = 0; j < n; j++)
{
if (!conbine[j] && !vist[j] && wet[j] > Max)
{
tmp = j;
Max = wet[j];
}
}
if (t == tmp) return;
s = t;
t = tmp;
mincut = Max;
vist[tmp] = 1;
for (j = 0; j < n; j++)
{
if (!conbine[j] && !vist[j])
{
wet[j] += map[tmp][j];
}
}
}
}
int stoer_wagner(){
int i, j;
memset(conbine, 0, sizeof(conbine));
int ans = INF;
for (i = 0; i < n - 1; i++){
search();
if (mincut < ans) ans = mincut;
if (ans == 0) return 0;
conbine[t] = 1;
for (j = 0; j < n; j++){
if (!conbine[j]){
map[s][j] += map[t][j];
map[j][s] += map[j][t];
}
}
}
return ans;
}
int main()
{
int a,b,c;
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(map,0,sizeof(map));
while(m--)
{
scanf("%d%d%d",&a,&b,&c);
map[a][b]+=c;
map[b][a]+=c;
}
int cnt=stoer_wagner();
cout<<cnt<<endl;
}
}