POJ - 2914 Minimum Cut

传送门

全局最小割

stoer_wagner

论打板选手的自我修养

 1 //Achen
 2 #include<algorithm>
 3 #include<iostream>
 4 #include<cstring>
 5 #include<cstdlib>
 6 #include<vector>
 7 #include<cstdio>
 8 #include<queue>
 9 #include<cmath>
10 const int N=507,inf=0x7fffffff;
11 typedef long long LL;
12 using namespace std;
13 int n,m,eg[N][N];
14 
15 template<typename T>void read(T &x)  {
16     char ch=getchar(); x=0; T f=1;
17     while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
18     if(ch=='-') f=-1,ch=getchar();
19     for(;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0'; x*=f;
20 }
21 
22 int vis[N],dis[N],id[N];
23 int stoer_wagner(int n) {
24     int res=inf;
25     for(int i=0;i<n;i++) id[i]=i;
26     while(n>1) {
27         memset(vis,0,sizeof(vis));
28         memset(dis,0,sizeof(dis));
29         int pr=0;
30         vis[id[pr]]=1;
31         for(int i=1;i<n;i++) {     
32             int k=-1;
33             for(int j=1;j<n;j++) if(!vis[id[j]]) {
34                 dis[id[j]]+=eg[id[pr]][id[j]];
35                 if(k==-1||dis[id[j]]>dis[id[k]]) k=j;
36             }
37             vis[id[k]]=1;
38             if(i==n-1) {
39                 res=min(res,dis[id[k]]);
40                 for(int j=0;j<n;j++) {
41                     eg[id[pr]][id[j]]+=eg[id[k]][id[j]];
42                     eg[id[j]][id[pr]]+=eg[id[j]][id[k]];
43                 }
44                 id[k]=id[--n];
45             }
46             pr=k;
47         }
48     }
49     return res;
50 }
51 
52 int main() {
53     while(scanf("%d%d",&n,&m)==2) {
54         memset(eg,0,sizeof(eg));
55         for(int i=1;i<=m;i++) {
56             int x,y,z;
57             read(x); read(y); read(z);
58             eg[x][y]+=z; eg[y][x]+=z;
59         }
60         printf("%d\n",stoer_wagner(n));
61     }
62     return 0;
63 }
View Code

 

posted @ 2018-02-23 08:10  啊宸  阅读(185)  评论(0编辑  收藏  举报