`

POJ 2914 Minimum Cut (Stoer_Wagner最小割)

阅读更多
Minimum Cut
Time Limit: 10000MS   Memory Limit: 65536K
Total Submissions: 6448   Accepted: 2686
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 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)
 
#include<iostream>
#include<cstdio>
#include<cstring>

using namespace std;

const int VM=520;
const int INF=0x3f3f3f3f;

int n,m,mincut,src,des;
int map[VM][VM],dis[VM],vis[VM],combine[VM];

void Search(){   //最大生成树
    int i,j,k;
    memset(vis,0,sizeof(vis));
    memset(dis,0,sizeof(dis));
    src=des=-1;
    int tmp;
    for(i=0;i<n;i++){
        tmp=-INF;
        for(j=0;j<n;j++)
            if(!combine[j] && !vis[j] && tmp<dis[j]){
                k=j;
                tmp=dis[j];
            }
        if(k==des)
            return ;
        src=des;  des=k;    //最后两个扩展的顶点。
        /*
        printf("---> i=%d   k=%d\n",i,k);
        printf("@@@@@@@@@@@@@@@\n");
        for(j=0;j<n;j++)
            printf("%d ",dis[j]);
        printf("\n");
        printf("@@@@@@@@@@@@@@@\n");
        */
        mincut=dis[k];
        vis[k]=1;
        for(j=0;j<n;j++)
            if(!combine[j] && !vis[j])
                dis[j]+=map[k][j];
    }
}

int Stoer_Wagner(){
    memset(combine,0,sizeof(combine));
    int ans=INF;  //min=MAXINT,固定一个顶点P
    for(int i=0;i<n-1;i++){
        Search();   //从点P用“类似”prim的s算法扩展出“最大生成树”,记录最后扩展的顶点和最后扩展的边
        /*
        printf("---------------\n");
        printf("i=%d  :   ",i);
        for(int j=0;j<n;j++)
            printf("%d ",dis[j]);
        printf("\n");
        printf("---------------\n");
        */
        ans=min(ans,mincut);    //计算最后扩展到的顶点的切割值(即与此顶点相连的所有边权和),若比min小更新min
        if(ans==0)  //图不连通时最小割为0
            return 0;
        combine[des]=1;
        for(int j=0;j<n;j++)    //合并最后扩展的那条边的两个端点为一个顶点
            if(!combine[j]){
                map[src][j]+=map[des][j];
                map[j][src]+=map[j][des];
            }
    }
    return ans;
}

int main(){

    //freopen("input.txt","r",stdin);

    while(~scanf("%d%d",&n,&m)){
        memset(map,0,sizeof(map));
        int u,v,w;
        while(m--){
            scanf("%d%d%d",&u,&v,&w);
            map[u][v]+=w;
            map[v][u]+=w;
        }
        printf("%d\n",Stoer_Wagner());
    }
    return 0;
}
 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics