zoukankan      html  css  js  c++  java
  • poj2914 Minimum Cut 全局最小割模板题

    Minimum Cut
    Time Limit: 10000MS   Memory Limit: 65536K
    Total Submissions: 8324   Accepted: 3488
    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)
     
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    #define MAXN 555
    #define inf 1<<30
    
    int v[MAXN],dist[MAXN];
    int map[MAXN][MAXN];
    bool vis[MAXN];
    int n,m;
    
    //求全局最小割的Stoer_Wanger算法
     int Stoer_Wanger(int n)
    {
         int res=inf;
        for(int i=0;i<n;i++)v[i]=i;
        while(n>1){
            int k=0,pre=0;//pre用来表示之前加入A集合的点,我们每次都以0点为第一个加入A集合的点
            memset(vis,false,sizeof(vis));
            memset(dist,0,sizeof(dist));
            for(int i=1;i<n;i++){
                k=-1;
                for(int j=1;j<n;j++){
                    if(!vis[v[j]]){
                        dist[v[j]]+=map[v[pre]][v[j]];//dis数组用来表示该点与A集合中所有点之间的边的长度之和
                        if(k==-1||dist[v[k]]<dist[v[j]]){
                            k=j;
                        }
                    }
                }
                vis[v[k]]=true;
                if(i==n-1){
                    res=min(res,dist[v[k]]);
                    //将该点合并到pre上,相应的边权就要合并
                    for(int j=0;j<n;j++){
                        map[v[pre]][v[j]]+=map[v[j]][v[k]];
                        map[v[j]][v[pre]]+=map[v[j]][v[k]];
                    }
                    v[k]=v[--n];//删除最后一个点
                }
                pre=k;
            }
        }
        return res;
    }
    
    int main()
    {
        int u,v,w,ss;
        while(~scanf("%d%d",&n,&m)){
    
            memset(map,0,sizeof(map));
            while(m--){
                scanf("%d%d%d",&u,&v,&w);
    
                map[u][v]+=w;
                map[v][u]+=w;
            }
            int ans=Stoer_Wanger(n);
            printf("%d
    ",ans);
        }
        return 0;
    }
     
  • 相关阅读:
    poj 1080 ——Human Gene Functions——————【最长公共子序列变型题】
    hdu 2845——Beans——————【dp】
    nyoj 214——单调递增子序列(二)——————【二分搜索加dp】
    nyoj 104——最大和——————【子矩阵最大和】
    nyoj 10——skiing————————【记忆化搜索】
    >>8) & 0xFF中的 >> 和 &0xFF 的作用
    C语言中的左移与右移 <<, >> 位运算
    ByPass Mode(略过模式或旁路模式)
    C语言&C++ 中External dependencies
    xor 和 or 有什么区别
  • 原文地址:https://www.cnblogs.com/13224ACMer/p/4726253.html
Copyright © 2011-2022 走看看