POJ 2914 Minimum Cut //无向图求最小割

Minimum Cut
Time Limit: 10000MS Memory Limit: 65536K
Total Submissions: 3610 Accepted: 1340
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

Source

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

 

#include<cstdio>
#include<cstring>
#define NN 510 //Maximum number of vertices in the graph
#define MAXW 250010// Maximum edge weight
int g[NN][NN],belongs[NN],w[NN],na[NN];
bool vis[NN];
int minCut(int n)
{
    for(int i=0;i<n;i++) belongs[i]=i;
    int best=0x7fffffff;
    while(n>1)//若|V|!=1则继续
    {
        vis[belongs[0]]=true;
        for(int i=1;i<n;i++)
        {
            vis[belongs[i]]=false;
            na[i-1]=i;
            w[i]=g[belongs[0]][belongs[i]];
        }//设最小割cut=INF,任选一个点s到集合A中,定义W(A, p)为A中的所有点到A外一点p的权总和.
        int prev=belongs[0];
        for(int i=1;i<n;i++)
        {
            int zj=-1;
            for(int j=1;j<n;j++)
                if(!vis[belongs[j]]&&(zj<0 || w[j]>w[zj]))  zj=j;
            //选出A外一点p, 且W(A,p)最大的作为新的s, 若A!=G(V), 则继续
            vis[belongs[zj]] = true;
            if(i==n-1)
            {
                if(best>w[zj]) best=w[zj];
                for(int i=0;i<n;i++)
                    g[belongs[i]][prev]=g[prev][belongs[i]]+=g[belongs[zj]][belongs[i]];
                //新建顶点u, 边权w(u, v)=w(s, v)+w(t, v), 删除顶点s和t, 以及与它们相连的边
                belongs[zj]=belongs[--n];
                break;
            }
            prev=belongs[zj];
            //对刚才选定的s, 更新W(A,p)(该值递增).
            for(int j=1;j<n;j++) if(!vis[belongs[j]]) w[j]+=g[belongs[zj]][belongs[j]];
        }
    }
    return best;
}
int main()
{
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        memset(g,0,sizeof(g));
        for(int i=0;i<m;i++)
        {
            int x,y,c;
            scanf("%d%d%d",&x,&y,&c);
            g[y][x]+=c;
            g[x][y]+=c;
        }
        printf("%d/n",minCut(n));
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值