POJ_2914_Minimum_Cut_(Stoer_Wagner)

描述


http://poj.org/problem?id=2914

求无向图中最小割.

 

Minimum Cut
Time Limit: 10000MS   Memory Limit: 65536K
Total Submissions: 8679   Accepted: 3659
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 ≤ MN × (N − 1) ⁄ 2) in one line, where N is the number of vertices. Following are M lines, each line contains M integers A, B and C (0 ≤ A, B < N, AB, 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
Wang, Ying (Originator)
Chen, Shixi (Test cases)

 

分析


不会做啊...

可以暴力枚举源点和汇点,然后开始瞎搞...必定超时啊...

有专门解决这种问题的算法: Stoer_Wagner.

好吧其实我并没有理解是为啥......感觉只知道算法思路.

设所要求的最小割为Cut.先找任意s,t的最小割,如果s,t在Cut两侧,则割(s,t)就是Cut,否则割(s,t)>=Cut,并且将s,t合成一个点不会影响Cut.就这样,我们每次找任意的s,t的割,然后合并.在找到分居Cut两侧的s,t之前,合并对结果没有影响,也就是说Cut还在图中.当某一步找到s,t分居在Cut两侧的时候,那一步的割(s,t)就是Cut,如果直到最后一步前还没有出现这种情况,最后一步只有两个点,只有一个割,又因为Cut一定在图中,所以图中的割就是Cut,综上,一定能找到Cut.但是我们不知道是哪一步找到的,所以记录一个min值就好了.

 

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #define for1(i,a,n) for(int i=(a);i<=(n);i++)
 5 #define for2(i,a,n) for(int i=(a);i<n;i++)
 6 #define read(a) a=getnum()
 7 #define CC(i,a) memset(i,a,sizeof(i))
 8 using namespace std;
 9 
10 const int maxn=500+5,INF=0x7fffffff;
11 int n,m;
12 int v[maxn],w[maxn];
13 bool vis[maxn];
14 int g[maxn][maxn];
15 
16 inline int getnum() { int r=0;char c;c=getchar();while(c<'0'||c>'9') c=getchar();while(c>='0'&&c<='9') {r=r*10+c-'0';c=getchar();}return r; }
17 
18 int stoer_wagner(int n)
19 {
20     int min_cut=INF;
21     for1(i,1,n) v[i]=i;
22     while(n>1)
23     {
24         int pre=1;
25         CC(vis,0);
26         CC(w,0);
27         for2(i,1,n)
28         {
29             int k=-1;
30             for1(j,2,n)
31             {
32                 if(!vis[v[j]])
33                 {
34                     w[v[j]]+=g[v[j]][v[pre]];
35                     if(k==-1||w[v[j]]>w[v[k]])
36                     {
37                         k=j;
38                     }
39                 }
40             }
41             vis[v[k]]=true;
42             
43             if(i==n-1)
44             {
45                 const int s=v[pre],t=v[k];
46                 min_cut=min(min_cut,w[t]);
47                 for1(j,1,n)
48                 {
49                     g[s][v[j]]+=g[t][v[j]];
50                     g[v[j]][s]+=g[t][v[j]];
51                 }
52                 v[k]=v[n--];
53             }
54             pre=k;
55         }
56     }
57     return min_cut;
58 }
59 
60 int main()
61 {
62 #ifndef ONLINE_JUDGE
63     freopen("min.in","r",stdin);
64     freopen("min.out","w",stdout);
65 #endif
66     while(scanf("%d%d",&n,&m)!=EOF)
67     {
68         CC(g,0);
69         while(m--)
70         {
71             int u,v,w;
72             read(u); read(v); read(w);
73             u++; v++;
74             g[u][v]+=w;
75             g[v][u]+=w;
76         }
77         printf("%d\n",stoer_wagner(n));
78     }
79 #ifndef ONLINE_JUDGE
80     fclose(stdin);
81     fclose(stdout);
82     system("min.out");
83 #endif
84     return 0;
85 }
86     
View Code

 

posted @ 2016-04-29 09:56  晴歌。  阅读(325)  评论(0编辑  收藏  举报