Smallest Minimum Cut
Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 471 Accepted Submission(s): 172
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 .
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
题意:给点一个有向图(n个点(n<=200)m条边(m<=1000)),求从s到t的最小割的最小边数。
思路:将每条边权值扩大大数MOD倍再加上1,所求最大流膜MOD即为答案。跑完最大流,假设在乘大数不加一的情况下,边容量为0即为割边,加1后,多跑的最大流即为最小割边的最小边数。(很奇怪,我用另外一种,先跑完最大流,然后根据容量新建边(cap为0建1,否则为INF)一直wa)
ACcode(Dinic)
#include<bits/stdc++.h>
using namespace std;
const int N=1000;
const int MOD=100000;
const int INF=0x3f3f3f3f;
struct edge
{
int to,cap,rev;
edge(int x,int y,int z)
{
to=x;cap=y;rev=z;
}
};
vector<edge> g[N];
int level[N],iter[N],n,m,s,t;
bool vis[N];
void add(int from,int to,int cap)
{
g[from].push_back(edge(to,cap,g[to].size()));
g[to].push_back(edge(from,0,g[from].size()-1));
}
void bfs(int s)
{
memset(level,-1,sizeof(level));
level[s]=0;
queue<int> que;
que.push(s);
while(!que.empty())
{
int v=que.front();que.pop();
for(int i=0;i<g[v].size();i++)
{
edge &e=g[v][i];
if(e.cap>0&&level[e.to]<0)
{
level[e.to]=level[v]+1;
que.push(e.to);
}
}
}
}
int dfs(int v,int t,int f)
{
if(v==t)return f;
for(int &i=iter[v];i<g[v].size();i++)
{
edge &e=g[v][i];
if(e.cap>0&&level[e.to]>level[v])
{
int d=dfs(e.to,t,min(f,e.cap));
if(d>0)
{
e.cap-=d;
g[e.to][e.rev].cap+=d;
return d;
}
}
}
return 0;
}
int max_flow(int s,int t)
{
int flow=0;
while(1)
{
bfs(s);
if(level[t]<0)return flow;
memset(iter,0,sizeof(iter));
int f;
while(f=dfs(s,t,INF))
{
flow+=f;
}
}
}
int main()
{
int TA,x,y,z;
scanf("%d",&TA);
while(TA--)
{
scanf("%d%d%d%d",&n,&m,&s,&t);
for(int i=1;i<=n;i++)
g[i].clear();
for(int i=1;i<=m;i++)
{
scanf("%d%d%d",&x,&y,&z);
z=z*MOD+1;
add(x,y,z);
}
printf("%d\n",max_flow(s,t)%MOD);
}
return 0;
}