| Time Limit: 10000MS | Memory Limit: 65536K | |
| Total Submissions:10457 | Accepted: 4372 | |
| 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 M 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
全局最小割模板:
#include<cstdio>
#include<string.h>
#include<algorithm>
using namespace std;
#define inf 0xf3f3f3f
const int maxn=505;
int rode[maxn][maxn],d[maxn];
bool is[maxn],bin[maxn];
int n,m;
int merge(int &s,int &t)
{
memset(is,0,sizeof(is));
memset(d,0,sizeof(d));
int ans;
while(1)
{
int v=-1;
for(int i=0;i<n;i++)
if(!bin[i]&&!is[i]&&(v==-1||d[i]>d[v]))v=i;
if(v==-1)return ans;
is[v]=1;
ans=d[v];
s=t,t=v;
for(int i=0;i<n;i++)
if(!bin[i]&&!is[i])d[i]+=rode[v][i];
}
}
int stoer_wagner()
{
int mincut=inf,s,t,ans;
for(int i=1;i<n;i++)
{
ans=merge(s,t);
bin[t]=1;
mincut=min(mincut,ans);
if(mincut==0)return 0;
for(int j=0;j<n;j++)
if(!bin[j])rode[j][s]=rode[s][j]=rode[s][j]+rode[t][j];
}
return mincut;
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
memset(rode,0,sizeof(rode));
memset(bin,0,sizeof(bin));
for(int i=0;i<m;i++)
{
int x,y,z;scanf("%d%d%d",&x,&y,&z);
rode[x][y]+=z;
rode[y][x]+=z;
}
printf("%d\n",stoer_wagner());
}
return 0;
}
本文介绍了一种解决无向图中最小割问题的算法实现,该算法能够计算至少需要移除多少条边来将图断开成两个子图。通过多次合并节点并更新边权重的方式找到全局最小割。
1万+

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



