Minimum Cut
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 |
———————————————————————————————————
题目的意思是给出一张图,求整体最小割
思路:模板题
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
using namespace std;
#define LL long long
const int INF = 0x3f3f3f3f;
#define MAXN 800
int mp[MAXN][MAXN],w[MAXN],vis[MAXN],combine[MAXN];
int minC,cnt,n,m;
void Find(int &s,int &t)
{
memset(vis,0,sizeof(vis));
memset(w,0,sizeof(w));
int tmp=INF;
int q=n;
while(q--)
{
int mx=-INF;
for(int i=0; i<n; i++)
{
if(!vis[i]&&!combine[i]&&mx<w[i])
{
mx=w[i];
tmp=i;
}
}
if(t==tmp)
{
minC=w[t];
return;
}
vis[tmp]=1;
s=t,t=tmp;
for(int i=0;i<n;i++)
{
if(!vis[i]&&!combine[i])
w[i]+=mp[t][i];
}
}
minC=w[t];
}
int mincut()
{
int ans=INF;
int s,t;
memset(combine,0,sizeof(combine));
for(int i=0; i<n-1; i++)
{
s=t=-1;
Find(s,t);
combine[t]=true;
ans=min(ans,minC);
for(int i=0;i<n;i++)
{
mp[s][i]+=mp[t][i];
mp[i][s]+=mp[i][t];
}
}
return ans;
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
memset(mp,0,sizeof(mp));
int u,v,w;
while(m--)
{
scanf("%d %d %d",&u,&v,&w);
mp[u][v]+=w;
mp[v][u]+=w;
}
printf("%d\n",mincut());
}
return 0;
}