poj 2914 Minimum Cut (全局最小割)

传送门: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;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值