Problem Description
Consider a network G=(V,E) with source s and sink t. An s-t cut is a partition of nodes set V into two parts such that s and t belong to different parts. The cut set is the subset of E with all edges connecting nodes in different parts. A minimum cut is the one whose cut set has the minimum summation of capacities. The size of a cut is the number of edges in the cut set. Please calculate the smallest size of all minimum cuts.
Input
The input contains several test cases and the first line is the total number of cases T (1≤T≤300).
Each case describes a network G, and the first line contains two integers n (2≤n≤200) and m (0≤m≤1000) indicating the sizes of nodes and edges. All nodes in the network are labelled from 1 to n.
The second line contains two different integers s and t (1≤s,t≤n) corresponding to the source and sink.
Each of the next m lines contains three integers u,v and w (1≤w≤255) describing a directed edge from node u to v with capacity w.
Output
For each test case, output the smallest size of all minimum cuts in a line.
Sample Input
2
4 5
1 4
1 2 3
1 3 1
2 3 1
2 4 1
3 4 2
4 5
1 4
1 2 3
1 3 1
2 3 1
2 4 1
3 4 3
Sample Output
2
3
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#define LL long long
#define inf 0x3f3f3f3f
using namespace std;
const int maxn=210;
struct node
{
int u,v,w,next;
}edge[maxn*20];
int n,m,sp,tp,k;
int head[maxn];
int d[maxn];
void addedge(int u,int v,int w)
{
edge[k].u=u;
edge[k].v=v;
edge[k].w=w;
edge[k].next=head[u];
head[u]=k++;
edge[k].u=v;
edge[k].v=u;
edge[k].w=0;
edge[k].next=head[v];
head[v]=k++;
}
int bfs()
{
queue<int> q;
memset(d,-1,sizeof(d));
d[sp]=0;
q.push(sp);
while(!q.empty())
{
int a=q.front();
q.pop();
for(int i=head[a];i!=-1;i=edge[i].next)
{
int b=edge[i].v;
if(d[b]==-1&&edge[i].w>0)
{
d[b]=d[a]+1;
q.push(b);
}
}
}
return d[tp]!=-1;
}
LL dfs(int a,LL b)
{
LL r=0;
if(a==tp)return b;
for(int i=head[a];i!=-1&&r<b;i=edge[i].next)
{
int u=edge[i].v;
if(edge[i].w>0&&d[u]==d[a]+1)
{
int x=min((LL)edge[i].w,b-r);
x=dfs(u,x);
r+=x;
edge[i].w-=x;
edge[i^1].w+=x;
}
}
if(!r)d[a]=-2;
return r;
}
LL dinic(int sp,int tp)
{
LL sum=0,t;
while(bfs())
{
while(t=dfs(sp,inf))
{
sum+=t;
}
}
return sum;
}
int main()
{
int T;
int u,v,w;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
scanf("%d%d",&sp,&tp);
k=0;
memset(head,-1,sizeof(head));
for(int i=0;i<m;i++)
{
scanf("%d%d%d",&u,&v,&w);
addedge(u,v,w*1000+1);
}
printf("%lld\n",dinic(sp,tp)%1000);
}
return 0;
}
本文介绍了一个网络中寻找最小割的方法,通过使用dinic算法来计算源点到汇点的最小割大小。文章提供了完整的C++代码实现,并通过样例输入输出展示了算法的应用。
213

被折叠的 条评论
为什么被折叠?



