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 casesT (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 from1 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
题目大意:给定一个有向图,求边数最小的最小割。
青岛网赛的一道题目,万万没想到是原题啊。。。并不会做,赛后百度了一下就吐血了。。。
先跑一次最大流,最小割中的边是满流边的子集(注:满流边不一定都是最小割中的边)。改一下图,满流的边流量改为1,不满流的流量改为INF,再跑一次,该最小割对应的就是最小割的边数。题解上本来说反向边要初始化的,但是一直 WA,把反向边的初始化去掉以后就 A 了。先把代码挂这儿,抽时间再想想吧。
代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
#include<vector>
using namespace std;
typedef struct node
{
int from,to,cap,flow;
node(int f = 0,int t = 0,int c = 0,int ff = 0):from(f),to(t),cap(c),flow(ff){}
}node;
const int INF = 1000000;
const int maxn = 200 + 5;
int n,s,t;
vector<node> edge;
vector<int> G[maxn];
int cur[maxn],di[maxn];
bool vis[maxn];
void addedge(int from,int to,int cap,int flow)
{
edge.push_back(node(from,to,cap,0));
edge.push_back(node(to,from,0,0));
int m = edge.size();
G[from].push_back(m-2);
G[to].push_back(m-1);
}
bool BFS()
{
int x;
memset(vis,false,sizeof(vis));
queue<int> q;
q.push(s);
vis[s] = true;
di[s] = 0;
while(!q.empty())
{
x = q.front();
q.pop();
for(int i = 0;i < G[x].size(); ++i)
{
node &e = edge[G[x][i]];
if(e.cap > e.flow && !vis[e.to])
{
di[e.to] = di[x] + 1;
vis[e.to] = true;
q.push(e.to);
}
}
}
return vis[t];
}
int DFS(int x,int a)
{
int flow = 0,f;
if(x == t || a == 0) return a;
for(int &i = cur[x];i< G[x].size(); ++i)
{
node &e = edge[G[x][i]];
if(di[e.to] == di[x] + 1 && (f = DFS(e.to,min(a,e.cap - e.flow))) > 0)
{
e.flow += f;
edge[G[x][i]^1].flow -= f;
flow += f;
a -= f;
if(a == 0) break;
}
}
return flow;
}
int maxflow()
{
int flow = 0;
while(BFS())
{
memset(cur,0,sizeof(cur));
flow += DFS(s,INF);
}
return flow;
}
void Init()
{
edge.clear();
for(int i = 0;i <= n; ++i) G[i].clear();
}
int main()
{
int T;
int x,y,z,m;
scanf("%d",&T);
while(T--)
{
scanf("%d %d %d %d",&n,&m,&s,&t);
Init();
for(int j = 0;j < m; ++j)
{
scanf("%d %d %d",&x,&y,&z);
addedge(x,y,z,0);
}
maxflow();
int len = edge.size();
for(int i = 0;i < len; i += 2)
{
if(edge[i].cap == edge[i].flow)
{
edge[i].cap = 1;
edge[i].flow = 0;
}
else
{
edge[i].cap = INF;
edge[i].flow = 0;
}
}
printf("%d\n",maxflow());
}
return 0;
}
本文介绍了一种求解有向图中边数最小的最小割的方法。通过两次运行最大流算法,首先找出最大流,然后调整边的容量再次运行算法以确定最小割中的边数。

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



