Sabotage UVA - 10480(最小割输出路径)

The regime of a small but wealthy dictatorship has been abruptly overthrown by an unexpected rebellion.
Because of the enormous disturbances this is causing in world economy, an imperialist military
super power has decided to invade the country and reinstall the old regime.
For this operation to be successful, communication between the capital and the largest city must
be completely cut. This is a difficult task, since all cities in the country are connected by a computer
network using the Internet Protocol, which allows messages to take any path through the network.
Because of this, the network must be completely split in two parts, with the capital in one part and
the largest city in the other, and with no connections between the parts.
There are large differences in the costs of sabotaging different connections, since some are much
more easy to get to than others.
Write a program that, given a network specification and the costs of sabotaging each connection,
determines which connections to cut in order to separate the capital and the largest city to the lowest
possible cost.
Input
Input file contains several sets of input. The description of each set is given below.
The first line of each set has two integers, separated by a space: First one the number of cities, n in
the network, which is at most 50. The second one is the total number of connections, m, at most 500.
The following m lines specify the connections. Each line has three parts separated by spaces: The
first two are the cities tied together by that connection (numbers in the range 1 − n). Then follows the
cost of cutting the connection (an integer in the range 1 to 40000000). Each pair of cites can appear
at most once in this list.
Input is terminated by a case where values of n and m are zero. This case should not be processed.
For every input set the capital is city number 1, and the largest city is number 2.
Output
For each set of input you should produce several lines of output. The description of output for each set
of input is given below:
The output for each set should be the pairs of cities (i.e. numbers) between which the connection
should be cut (in any order), each pair on one line with the numbers separated by a space. If there is
more than one solution, any one of them will do.
Print a blank line after the output for each set of input.
Sample Input
5 8
1 4 30
1 3 70
5 3 20
4 3 5
4 5 15
5 2 10
3 2 25
2 4 50
5 8
1 4 30
1 3 70
5 3 20
4 3 5
4 5 15
5 2 10
3 2 25
2 4 50
0 0
Sample Output
4 1
3 4
3 5
3 2
4 1
3 4
3 5
3 2
给定一个n个节点,m条路径的无向图,源点为1,汇点为2,求这张图的最小割的割法。
根据最大流最小割定理,一张图的最大流等于最小割,但是最大流的路径和最小割路径不一样,所以输出路径时,对残余网络遍历一遍,把源点的集合设置为1,汇点的集合设置为0,如果两点连通但值不相同,这条路径即为最小割路径

#include<stdio.h>
#include<iostream>
#include<cmath>
#include<queue>
#include<cstring>
using namespace std;
const int INF=0x3f3f3f3f;
const int maxn=1e4+5;
const int maxm=1e5+5;
int n,m,S,T,ne,head[maxn],cur[maxn],depth[maxn],maxflow;
int vis[100];
queue<int> q;
struct edge
{
	int next,to,cost,us,cs;
}e[maxm*2];
void init()
{
	memset(head,-1,sizeof(head));
	memset(vis,0,sizeof(vis));
	ne=0;
	maxflow=0;
}
void add(int u,int v,int w)
{
	e[ne].to=v;
	e[ne].cost=w;
	e[ne].next=head[u];
	e[ne].us=u;
	e[ne].cs=w;
	head[u]=ne++;
	e[ne].to=u;
	e[ne].cost=w;
	e[ne].next=head[v];
	e[ne].us=v;
	e[ne].cs=w;
	head[v]=ne++;
}
int bfs(int s,int t)
{
	while(!q.empty()) q.pop();
	memset(depth,-1,sizeof(depth));
	for(int i=1;i<=n;i++) cur[i]=head[i];
	depth[s]=0;
	q.push(s);
	while(!q.empty())
	{
		int u=q.front();
		q.pop();
		for(int i=head[u];i!=-1;i=e[i].next)
		{
			if(depth[e[i].to]==-1&&e[i].cost)
			{
				depth[e[i].to]=depth[u]+1;
				q.push(e[i].to);
			}
		}
	}
	if(depth[t]==-1) return -1;
	else return 1;
}
int dfs(int s,int t,int limit)
{
	if(!limit||s==t) return limit;
	int f,flow=0;
	for(int i=cur[s];i!=-1;i=e[i].next)
	{
		cur[s]=i;
		if(depth[e[i].to]==depth[s]+1&&(f=dfs(e[i].to,t,min(limit,e[i].cost))))
		{
			flow+=f;
			limit-=f;
			e[i].cost-=f;
			e[i^1].cost+=f;
			if(!limit) break;
		}
	}
	return flow;
}
void dinic(int s,int t)
{
	while(bfs(s,t)!=-1)
	{
		maxflow+=dfs(s,t,INF);
	}
}
void fi(int x)
{
	vis[x]=1;
	while(!q.empty()) q.pop();
	q.push(x);
	while(!q.empty())
	{
		int u=q.front();
		q.pop();
		for(int i=head[u];i!=-1;i=e[i].next)
		{
			if(e[i].cost&&vis[e[i].to]==0)
			{
				vis[e[i].to]=1;
				q.push(e[i].to);
			}
		}
	}
}
int main()
{
	while(~scanf("%d %d",&n,&m)&&(n+m))
	{
		init();
		S=1;
		T=2;
		for(int i=0;i<m;i++)
		{
			int x,y,z;
			scanf("%d%d%d",&x,&y,&z);
			add(x,y,z);
		}
		dinic(S,T);
		fi(S);
		for(int i=1;i<=n;i++)
		{
			for(int j=head[i];j!=-1;j=e[j].next)
			{
				if(vis[i]+vis[e[j].to]==1&&i<e[j].to) printf("%d %d\n",i,e[j].to);
			}
		}
		puts("");
	}
}

猜你喜欢

转载自blog.csdn.net/zufe_cst/article/details/86509237