1. 程式人生 > >HDU - 3002 King of Destruction(最小割)

HDU - 3002 King of Destruction(最小割)

ifdef main pac class nbsp return cas i++ efi

http://acm.hdu.edu.cn/showproblem.php?pid=3002

最小割模板

#include<iostream>
#include<cmath>
#include<cstring>
#include<queue>
#include<vector>
#include<cstdio>
#include<algorithm>
#include<map>
#include<set>
#define rep(i,e) for(int i=0;i<(e);i++)
#define
rep1(i,e) for(int i=1;i<=(e);i++) #define repx(i,x,e) for(int i=(x);i<=(e);i++) #define X first #define Y second #define PB push_back #define MP make_pair #define mset(var,val) memset(var,val,sizeof(var)) #define scd(a) scanf("%d",&a) #define scdd(a,b) scanf("%d%d",&a,&b) #define
scddd(a,b,c) scanf("%d%d%d",&a,&b,&c) #define pd(a) printf("%d\n",a) #define scl(a) scanf("%lld",&a) #define scll(a,b) scanf("%lld%lld",&a,&b) #define sclll(a,b,c) scanf("%lld%lld%lld",&a,&b,&c) #define IOS ios::sync_with_stdio(false);cin.tie(0) using namespace
std; typedef long long ll; template <class T> void test(T a){cout<<a<<endl;} template <class T,class T2> void test(T a,T2 b){cout<<a<<" "<<b<<endl;} template <class T,class T2,class T3> void test(T a,T2 b,T3 c){cout<<a<<" "<<b<<" "<<c<<endl;} const int N = 1e6+10; const int inf = 0x3f3f3f3f; const ll INF = 0x3f3f3f3f3f3f3f3fll; const ll mod = 1000000007; int T; void testcase(){ printf("Case #%d: ",++T); } const int MAXN = 2500; const int MAXM = 30; int maz[MAXN][MAXN]; int v[MAXN],dist[MAXN]; bool visit[MAXN]; int n,m; int StoerWagner(int n){ int cut = inf; for(int i=0;i<n;i++) v[i]=i; while(n>1){ int k=1,pre=0; for(int i=1;i<n;++i){ dist[v[i]]=maz[v[0]][v[i]]; if(dist[v[i]]>dist[v[k]]) k=i; } mset(visit,false); visit[v[0]]=true; for(int i=1;i<n;i++){ if(i==n-1){ cut=min(cut,dist[v[k]]); for(int j=0;j<n;j++){ maz[v[pre]][v[j]] += maz[v[j]][v[k]]; maz[v[j]][v[pre]] += maz[v[j]][v[k]]; } v[k]=v[--n]; } visit[v[k]]=true; pre = k,k=-1; for(int j=1;j<n;j++){ if(!visit[v[j]]){ dist[v[j]] += maz[v[pre]][v[j]]; if(k==-1 || dist[v[k]]<dist[v[j]]) k=j; } } } } return cut; } int main() { #ifdef LOCAL freopen("in.txt","r",stdin); #endif // LOCAL int n,m; while(~scdd(n,m)){ mset(maz,0); int x,y,w; for(int i=0;i<m;i++){ scddd(x,y,w); maz[x][y]+=w; maz[y][x]+=w; } cout<<StoerWagner(n)<<endl; } return 0; }

HDU - 3002 King of Destruction(最小割)