POJ_P2914 Minimum Cut(Stoer-Wagner算法 全局最小割)

23 篇文章 0 订阅
23 篇文章 0 订阅

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

//Stoer-Wagner算法不断缩点
//日狗的感觉……想加个堆优化,先用STL,慢的要死,直接TLE,然后手打堆……噗还是TLE,还是朴素算法过的,噗噗噗!
//第一段注释STL打的优先队列,第二段是手打的堆,有兴趣的同学自己看看吧

#include<cstdio>
#include<climits>
#include<cstring>
#include<vector>
#include<queue>
#include<iostream>
using namespace std;
#define N 505
#define INF INT_MAX/3*2
struct Min_Cut{
    struct Dist{
        int s,d;
    /*  Dist(int a,int b):s(a),d(b){}
        bool operator <(const Dist &b)const{
            if(d==b.d) return s>b.s;
            return d<b.d;
        }*/
    };
    int mp[N][N];bool use[N];
    int n,m,ans,S,T;


    int in(){
        int x=0;char ch=getchar();
        while(ch>'9'||ch<'0') ch=getchar();
        while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
        return x;
    }

    void init(){
        memset(mp,0,sizeof(mp));memset(use,0,sizeof(use));
        int x,y,z;ans=INF;
        while(m--){
            x=in(),y=in(),z=in();
            mp[x][y]+=z;mp[y][x]+=z;
        }
        return;
    }

    int MinCut(int &s,int &t){
        bool vis[N];int w[N];
        memset(vis,0,sizeof(vis));memset(w,0,sizeof(w));
        int tmpj=N;
        for(int i=0;i<n;i++){
            int max=-INF;
            for(int j=0;j<n;j++){
                if(!vis[j]&&!use[j]&&max<w[j]){
                    max=w[j];
                    tmpj=j;
                }
            }
            if(t==tmpj){return w[t];}
            vis[tmpj]=1;
            s=t,t=tmpj;
            for(int j=0;j<n;j++){
                if(!vis[j]&&!use[j])
                    w[j]+=mp[t][j];
            }
        }
        return w[t];
    }

/*  int MinCut(int &S,int &T){
        priority_queue<Dist> q;queue<Dist> p;Dist t(0,0);
        for(int i=0;i<n;i++) 
            if(!use[i]) q.push(Dist(i,0));
        while(!q.empty()){
            t=q.top();q.pop();
            if(T==t.s){return t.d;}
            S=T;T=t.s;
            while(!q.empty()){
                p.push(q.top());q.pop();
            }
            while(!p.empty()){
                t=p.front();p.pop();
                q.push(Dist(t.s,mp[T][t.s]+t.d));
            }
        }
        return q.top().d;
    }*/

/*  
    Dist heap[N*2];int size;
    void push(Dist x){
        heap[++size]=x;
        int now,next;now=size,next=size/2;
        while(now>1){
            next=now/2;
            if(heap[next].d>heap[now].d) break;
            else swap(heap[next],heap[now]);
            now=next;
        }
        return;
    }

    Dist top(){
        Dist res=heap[1];heap[1]=heap[size--];
        int now,next;now=1;
        while(now*2<=size){
            next=now*2;
            if(next+1<=size&&heap[next].d>heap[next+1].d) next++;
            if(heap[now].d>heap[next].d)    break;
            swap(heap[now],heap[next]);
            now=next;
        }
        return res;
    }

    int MinCut(int &S,int &T){
        memset(heap,0,sizeof(heap));size=0;
        queue<Dist> p;Dist t;
        for(int i=0;i<n;i++) 
            if(!use[i]){
                t.s=i,t.d=0;
                push(t);
            }
        while(size){
            t=top();
            if(T==t.s){return t.d;}
            S=T;T=t.s;
            while(size)
                p.push(top());
            while(!p.empty()){
                t=p.front();p.pop();t.d+=mp[T][t.s];
                push(t);
            }
        }
        return top().d;
    }
    */

    void solve(){
        while(scanf("%d%d",&n,&m)!=EOF){
            init();
            for(int i=1;i<n;i++){
                S=T=-1;ans=min(ans,MinCut(S,T));use[T]=true;

                for(int j=0;j<n;j++){
                    mp[S][j]+=mp[T][j];
                    mp[j][S]+=mp[j][T];
                }
                /*for(int o=0;o<n;o++){
                    for(int u=0;u<n;u++){
                        printf("%d ",mp[o][u]);
                    }
                    printf("\n");
                }*/
//              printf("%d\n",ans);
            }
            printf("%d\n",ans);
        }
        return;
    }
}s;
int main(){
    s.solve();
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值