您的位置:首页 > 其它

POJ 2914 Minimum Cut

2014-10-02 10:35 218 查看

Minimum Cut

Time Limit: 10000ms
Memory Limit: 65536KB
This problem will be judged on PKU. Original ID: 2914
64-bit integer IO format: %lld Java class name: Main

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 areM 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

Source

Baidu Star 2006 Semifinal

解题:无向图的最小割。Stoer-Wagner 算法

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#include <stack>
#define LL long long
#define pii pair<int,int>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = 510;
int e[maxn][maxn],n,m;
bool comb[maxn];
int Find(int &s,int &t){
bool vis[maxn];
int w[maxn];
memset(vis,false,sizeof(vis));
memset(w,0,sizeof(w));
int tmp = INF;
for(int i = 0; i < n; ++i){
int theMax = -INF;
for(int j = 0; j < n; j++)
if(!vis[j] && !comb[j] && w[j] > theMax)
theMax = w[tmp = j];
if(t == tmp) break;
s = t;
vis[t = tmp] = true;
for(int j = 0; j < n; j++)
if(!vis[j] && !comb[j])
w[j] += e[t][j];
}
return w[t];
}
int solve(){
int ans = INF,s,t;
memset(comb,0,sizeof(comb));
for(int i = 1; i < n; i++){
s = t = -1;
ans = min(ans,Find(s,t));
for(int j = 0; j < n; j++){
e[s][j] += e[t][j];
e[j][s] += e[j][t];
}
comb[t] = true;
}
return ans;
}
int main() {
int u,v,w;
while(~scanf("%d %d",&n,&m)){
memset(e,0,sizeof(e));
while(m--){
scanf("%d %d %d",&u,&v,&w);
e[u][v] += w;
e[v][u] += w;
}
printf("%d\n",solve());
}
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: