传送门
存个板子:
代码:
#include<bits/stdc++.h>
#define ll long long
#define re register
#define cs const
using std::cerr;
using std::cout;
cs int N=607,INF=0x7fffffff;
int n,m,S,T;
bool vs[N],us[N];
int w[N][N],cut[N];
int shrink(){
int ans=INF,np=T;
memset(us,0,sizeof us);
memset(cut,0,sizeof cut);
for(int re i=1;i<=n;++i){
int mxc=-INF;
for(int re j=1;j<=n;++j)
if(!vs[j]&&!us[j]&&mxc<cut[j]){
np=j,mxc=cut[j];
}if(np==T)break;us[np]=true;ans=mxc,S=T,T=np;
for(int re j=1;j<=n;++j)if(!vs[j]&&!us[j])cut[j]+=w[T][j];
}return ans;
}
int stoer_wagner(){
int ans=INF;
for(int re i=1;i<n;++i){
ans=std::min(ans,shrink());
if(!ans)return 0;vs[T]=true;
for(int re j=1;j<=n;++j)
if(!vs[j]){
w[S][j]+=w[T][j];
w[j][S]+=w[j][T];
}
}return ans;
}
void work(){
scanf("%d%d",&n,&m);
for(int re i=1;i<=m;++i){
int u,v,vl;
scanf("%d%d%d",&u,&v,&vl);
w[u][v]+=vl,w[v][u]+=vl;
}
std::cout<<stoer_wagner()<<"\n";
}
void file(){
#ifdef zxyoi
freopen("min_cut.in","r",stdin);
#endif
}
signed main(){file();work();return 0;}