传送门:http://poj.org/problem?id=2914
给一个无向图,问最少切去几条边能使它变为非连通图。
stoer-wagner全局最小割模板题。
由于没有给定源点和汇点,因此用最大流最小割定理来用最大流枚举源点和汇点求解肯定会超时。
AC代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cstdlib>
#include<utility>
#include<algorithm>
#include<utility>
#include<queue>
#include<vector>
#include<set>
#include<stack>
#include<cmath>
#include<map>
#include<ctime>
#define P pair<int,int>
#define ll long long
#define ull unsigned long long
#define lson id*2,l,mid
#define rson id*2+1,mid+1,r
#define ls id*2
#define rs id*2+1
#define Mod(a,b) a<b?a:a%b+b
using namespace std;
const ll M = 1e9 + 7;
const ll INF = 1e9;
const int N = 510;
const double e = 10e-6;
int n, m;
int edge[N][N], dist[N];
bool vis[N], bin[N];
int S, T;
int merge()
{
memset(dist, 0, sizeof(dist));
memset(vis, false, sizeof(vis));
int mincut, next;
for (int i = 1; i <= n; i++){
int maxx = -INF;
for (int j = 1; j <= n; j++){
if (!bin[j] && !vis[j] && maxx < dist[j]){
next = j;
maxx = dist[j];
}
}
if (next == T)
break;
vis[next] = true;
mincut = maxx;
S = T, T = next;
for (int j = 1; j <= n; j++) {
if (bin[j] || vis[j])
continue;
dist[j] += edge[next][j];
}
}
return mincut;
}
int stoerWagner()
{
memset(bin, false, sizeof(bin));
int ans = INF;
for (int i = 1; i <= n - 1; i++){
ans = min(ans, merge());
if (ans == 0)
return 0;
bin[T] = true;
for (int j = 1; j <= n; j++)
if (!bin[j]) {
edge[S][j] += edge[T][j];
edge[j][S] += edge[j][T];
}
}
return ans;
}
int main()
{
while (~scanf("%d%d", &n, &m)) {
memset(edge, 0, sizeof(edge));
int x, y, c;
for (int i = 1; i <= m; i++){
scanf("%d%d%d", &x, &y, &c);
x++; y++;
edge[x][y] += c; edge[y][x] += c;
}
printf("%d\n", stoerWagner());
}
return 0;
}