|
Time Limit: 10000MS | Memory Limit: 65536K | |
| Total Submissions: 6414 | Accepted: 2670 | |
| 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, whereN is the number of vertices. Following are M lines, each line containsM integers A, B and C (0 ≤ A, B <N, A ≠ B, C > 0), meaning that there C edges connecting verticesA 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
#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<algorithm>
using namespace std;
#define MAXN 600
#define INF 0x3ffffff
int n,g[MAXN][MAXN];
int b[MAXN],dist[MAXN];
int Min_Cut_Phase(int ph,int &x,int &y)
{
int t=1;
b[t]=ph;
for(int i=1;i<=n;i++)
if(b[i]!=ph) dist[i]=g[1][i];
for(int i=1;i<n;i++)
{
x=t;t=0;
for(int j=1;j<=n;j++)
if(b[j]!=ph && (!t||dist[j]>dist[t])) t=j;
b[t]=ph;
for(int j=1;j<=n;j++)
if(b[j]!=ph) dist[j]+=g[t][j];
}
y=t;
return dist[t];
}
void Merge(int x,int y)
{
if(x>y) swap(x,y);
for(int i=1;i<=n;i++)
if(i!=x && i!=y)
g[i][x]+=g[i][y],g[x][i]+=g[i][y];
if(y==n) return;
for(int i=1;i<n;i++)
if(i!=y)
{
swap(g[i][y],g[i][n]);
swap(g[y][i],g[n][i]);
}
}
int Stoer_Wagner()
{
int ret=INF;
int x,y;
memset(b,0,sizeof(b));
for(int i=1;n>1;i++,n--)
{
ret=min(ret,Min_Cut_Phase(i,x,y));
Merge(x,y);
}
return ret;
}
int m;
int main()
{
while(~scanf("%d%d",&n,&m))
{
memset(g,0,sizeof(g));
int x,y,c;
for(int i=0;i<m;i++)
{
scanf("%d%d%d",&x,&y,&c);
x++,y++;
g[x][y]+=c;
g[y][x]=g[x][y];
}
printf("%d\n",Stoer_Wagner());
}
return 0;
}
本文介绍了一种解决无向图中最小割问题的方法。通过Stoer-Wagner算法,文章详细解释了如何找出使图断开所需的最少边数,特别关注了多重边的情况。输入包含多个测试案例,输出为每组案例最小割的大小。

1249

被折叠的 条评论
为什么被折叠?



