UVA 10480 Sabotage (最大流)

来源:互联网 发布:naca4412翼型数据 编辑:程序博客网 时间:2024/06/06 03:18

Sabotage

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, nin 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 81 4 301 3 705 3 204 3 54 5 155 2 103 2 252 4 505 81 4 301 3 705 3 204 3 54 5 155 2 103 2 252 4 500 0

Sample Output

4 13 43 53 24 13 43 53 2

题意是有n个点,m条边,要把1,2两点分开,问怎么分割才能使所用权值最小。

换一种方法思考,就是怎么样分割能使剩下的路径权值最大,这就是典型的网络流的最大流问题。bfs搜增广路,最后得到最大流,每个点相邻的边的流量为0时,就说明该点已经被去除,输出路径。

代码实现:

#include <stdio.h>#include <math.h>  #include <stdlib.h>  #include <string.h>  #include <time.h>  #include <iostream>  #include <algorithm> #include <string>#include <queue>   #include <stack>  #include <vector>#include <set>  #include <list>  #define mset(a,i) memset(a,i,sizeof(a))#define S1(n)    scanf("%d",&n)#define S2(n,m)  scanf("%d%d",&n,&m)#define P(n)     printf("%d\n",n);#define FIN      freopen("input.txt","r",stdin)#define FOUT     freopen("output.txt","w",stdout)using namespace std;typedef long long ll;const double eps=1e-6;const int INF=0x3f3f3f3f;const int mod=1e9+7;const int MAX=105;const double PI=acos(-1);int dir[5][2]={0,1,0,-1,1,0,-1,0};ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}ll qpow(ll n,ll m){n%=mod;ll ans=1;while(m){if(m%2) ans=(ans*n)%mod;m/=2;n=(n*n)%mod;}return ans;}ll inv(ll b){return b==1?1:(mod-mod/b)*inv(mod%b)%mod;}ll inv2(ll b){return qpow(b,mod-2);}int map[MAX][MAX],flow[MAX][MAX],path[MAX],a[MAX]; //map初始流量,flow最大流,path记录路径,a存i点在最大流中的流量int n,start,END,m;const int maxm=550;int x[maxm],y[maxm];int maxflow(){int i,j,k;queue <int> q;mset(flow,0);int max_flow=0;while(1){mset(a,0);                                 //清空标记 a[start]=INF;                              //默认源点权值无限大 while(!q.empty())                          //清空队列 q.pop();q.push(start);                             //源点入队 while(!q.empty())                          //队列非空 {int temp=q.front();                    //取队首 q.pop();                               //弹出 //if(temp==end)//break;for(i=1;i<=n;i++)                      //枚举每一个点 {if(!a[i]&&flow[temp][i]<map[temp][i])  //未走过且最大流小于权值 {path[i]=temp;                      //记录前一个点 a[i]=min(a[temp],map[temp][i]-flow[temp][i]);  //前一个点的流量或者当前点的流量的最小值 q.push(i);}}}if(a[END]==0)break;for(j=END;j!=start;j=path[j])       //更新剩余网络 {flow[path[j]][j]+=a[END];       //反向弧相加             flow[j][path[j]]-=a[END];       //通过的边相减 }max_flow+=a[END];                   //最大流权值 }return max_flow;}int main(){int i,j,k;while(~scanf("%d%d",&n,&m)){if(m==0&&n==0)break;mset(map,0);for(i=0;i<m;i++){scanf("%d%d",&x[i],&y[i]);scanf("%d",&map[x[i]][y[i]]);map[y[i]][x[i]]=map[x[i]][y[i]];}start=1;END=2;int x1=maxflow();for(i=0;i<m;i++)                 //枚举每一条边的两个点 {if((!a[x[i]]&&a[y[i]])||(a[x[i]]&&!a[y[i]]))    //有一点的权值为0,说明当前边被删除 printf("%d %d\n",x[i],y[i]);}printf("\n");}return 0;}