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 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两点分开,问怎么分割才能使所用权值最小。
思路:用最小割最大流定理就可以了,记得每组输出后有空行。
代码:
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <cmath>
#include <queue>
#include <vector>
using namespace std;
const int maxn=10010;
const int maxm=100010;
const int inf=0x3f3f3f3f;
int n,np,nc,m;
int s,t;
int cnt;
int Next[maxm];
int depth[maxn];
int head[maxn];
int cur[maxn];
int x[maxn];
int y[maxn];
struct edge{
int u,v,w;
}e[maxm];
void addedge(int u,int v,int w)
{
cnt++;
Next[cnt]=head[u];
head[u]=cnt;
e[cnt].u=u;
e[cnt].v=v;
e[cnt].w=w;
cnt++;
Next[cnt]=head[v];
head[v]=cnt;
e[cnt].u=v;
e[cnt].v=u;
e[cnt].w=w;
}
int bfs()
{
memset(depth,0,sizeof(depth));
depth[s]=1;
queue<int>q;
q.push(s);
while(!q.empty())
{
int u=q.front();
q.pop();
for(int i=head[u];i!=-1;i=Next[i])
{
int v=e[i].v;
if(depth[v]==0&&e[i].w>0)
{
depth[v]=depth[u]+1;
q.push(v);
}
}
}
if(depth[t]==0)return 0;
return 1;
}
int dfs(int u,int w)
{
if(u==t)
return w;
int f=0;
for(int i=head[u];i!=-1;i=Next[i])
{
int v=e[i].v;
if(depth[v]==depth[u]+1&&e[i].w>0)
{
int d=dfs(v,min(w-f,e[i].w));
e[i].w-=d;
e[i^1].w+=d;
f+=d;
if(f==w)break;
}
}
if(!f)depth[u]=0;
return f;
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
if(m==0&&n==0)break;
int i;
int w;
cnt=-1;
memset(head,-1,sizeof(head));
int a,b,c;
s=1;
t=2;
for(i=1;i<=m;i++)
{
scanf("%d%d%d",&a,&b,&c);
x[i]=a,y[i]=b;
addedge(a,b,c);
}
int ans=0;
while(bfs())
{
while(int d=dfs(s,inf))
{
ans+=d;
}
}
for(i=1;i<=m;i++)
{
if((!depth[x[i]]&&depth[y[i]])||(depth[x[i]]&&!depth[y[i]]))
{
printf("%d %d\n",x[i],y[i]);
}
}
printf("\n");
}
return 0;
}