POJ 2914 Minimum Cut

Minimum Cut
Time Limit: 10000MS   Memory Limit: 65536K
Total Submissions: 9319   Accepted: 3910
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 Mlines, each line contains M integers AB 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

Source

Baidu Star 2006 Semifinal 
Wang, Ying (Originator) 
Chen, Shixi (Test cases)

分析:

这道题也需要枚举...不过和求点连通度相比较,求边连通度的时候只需要任意选取源点,然后枚举汇点即可...

为何?

因为最小割可以把原图分成ST两个集合...我们选取了一个源点u,一定存在另一个点存在于另一个集合...

但是求点连通度的时候我们可能选取的两个点刚好是需要割的点...所以我们必须枚举所有的点对...

但是这是暴力的方法...还有另一种复杂度要低的做法--stoer_wagner

其算法的精髓在于不断合并点对信息从而缩小图的规模...

我们考虑最小割可以把原图分为两个集合ST...我们选取了st两个点,如果st位于一个集合,那么我们把st合并成一个点对答案是没有影响的...如果位于两个集合,那么求出的最小割就是答案...

所以我们先随便选取一个点扔入A集合,然从后当前点开始延伸,更新与当前点有边相连的点的权值(权值就是加上边权)...然后选取一个权值最大的点扔入A集合,继续更新...

然后到当只剩下最后一个点没有并入A集合的时候,我们就把这个点选为t点,把倒数第二个并入A集合的点选为s点,t的权值就是当前的最小割...然后合并st...再在新图上进行选点求最小割的操作...

代码:

 1 #include<algorithm>
 2 #include<iostream>
 3 #include<cstring>
 4 #include<cstdio>
 5 //by NeighThorn
 6 #define inf 0x3f3f3f3f
 7 using namespace std;
 8 //大鹏一日同风起,扶摇直上九万里 
 9 
10 const int maxn=500+5;
11 
12 int n,m,w[maxn],mp[maxn][maxn],bel[maxn],vis[maxn];
13 
14 inline int stoer_wagner(void){
15     int ans=inf;
16     for(int i=1;i<=n;i++)
17         bel[i]=i;
18     while(n>1){
19         memset(w,0,sizeof(w));
20         memset(vis,0,sizeof(vis));
21         int pre=1;vis[bel[pre]]=1;
22         for(int i=1;i<=n-1;i++){
23             int k=-1;
24             for(int j=2;j<=n;j++)
25                 if(!vis[bel[j]]){
26                     w[bel[j]]+=mp[bel[pre]][bel[j]];
27                     if(k==-1||w[bel[k]]<w[bel[j]])
28                         k=j;
29                 }
30             vis[bel[k]]=1;
31             if(i==n-1){
32                 int S=bel[pre],T=bel[k];
33                 ans=min(ans,w[T]);
34                 for(int j=1;j<=n;j++)
35                     mp[S][bel[j]]+=mp[bel[j]][T],mp[bel[j]][S]+=mp[bel[j]][T];
36                 bel[k]=bel[n];n--;
37             }
38             pre=k;
39         }
40     }
41     return ans==inf?0:ans;
42 }
43 
44 signed main(void){
45     while(scanf("%d%d",&n,&m)!=EOF){
46         memset(mp,0,sizeof(mp));
47         for(int i=1,s,x,y;i<=m;i++)
48             scanf("%d%d%d",&x,&y,&s),x++,y++,mp[x][y]+=s,mp[y][x]+=s;
49         printf("%d
",stoer_wagner());
50     }
51     return 0;
52 }//Cap ou pas cap. Cap.
View Code

By NeighThorn

原文地址:https://www.cnblogs.com/neighthorn/p/6232908.html