# UVA 10480 Sabotage （网络流，最大流，最小割）

### Description

The regime of a small but wealthy dictatorship has been abruptly overthrown by an unexpected rebel-lion. 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 speci cation 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

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

4 1
3 4
3 5
3 2

4 1
3 4
3 5
3 2

### Http

https://vjudge.net/problem/UVA-10480

## 代码

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;

const int maxN=60;
const int maxM=2000;
const int inf=2147483647;

class Edge
{
public:
int u,v,flow;
bool edge;
};

int n,m;
int cnt;
int Next[maxM];
Edge E[maxM];
int depth[maxN];
int cur[maxN];
int Q[maxM];
bool is_outp[maxN][maxN];
bool setS[maxN];

bool bfs();
int dfs(int u,int flow);
void outp(int u);

int main()
{
bool op=0;//用来标记是否是第一组数据，因为要在每两组数据之间输出一个空行
while (cin>>n>>m)
{
if ((n==0)&&(m==0))
break;
if (op)
printf("\n");
op=1;
cnt=-1;//初始化为-1
for (int i=1;i<=m;i++)
{
int u,w,v;
scanf("%d%d%d",&u,&v,&w);//连边
}
while (bfs())//求解最大流
{
for (int i=1;i<=n;i++)
while (dfs(1,inf));
}
memset(is_outp,0,sizeof(is_outp));//标记某对点是否已经输出
memset(setS,0,sizeof(setS));//标记某个点是否在集合S中
outp(1);//dfs求在集合S中的点
for (int i=1;i<=n;i++)
if (setS[i]==1)
{
{
int v=E[j].v;
if ((setS[v]==0)&&(is_outp[i][v]==0))//注意判重
{
printf("%d %d\n",i,v);
is_outp[i][v]=1;
}
}
}
/*
for (int i=0;i<=cnt;i++)
if ((E[i].flow==0)&&(E[i].edge==1))
printf("%d %d\n",E[i].u,E[i].v);
//*/
}
}

{
cnt++;
E[cnt].u=u;
E[cnt].v=v;
E[cnt].flow=flow;
E[cnt].edge=1;

cnt++;
E[cnt].v=u;
E[cnt].u=v;
E[cnt].flow=0;
E[cnt].edge=0;
}

bool bfs()
{
memset(depth,-1,sizeof(depth));
int h=1,t=0;
Q[1]=1;
depth[1]=1;
do
{
t++;
int u=Q[t];
{
int v=E[i].v;
if ((depth[v]==-1)&&(E[i].flow>0))
{
depth[v]=depth[u]+1;
h++;
Q[h]=v;
}
}
}
while (h!=t);
if (depth[2]==-1)
return 0;
/*for (int i=1;i<=n;i++)
cout<<depth[i]<<" ";
cout<<endl;
getchar();*/
return 1;
}

int dfs(int u,int flow)
{
//cout<<u<<endl;
if (u==2)
return flow;
for (int &i=cur[u];i!=-1;i=Next[i])
{
int v=E[i].v;
if ((depth[v]==depth[u]+1)&&(E[i].flow>0))
{
//cout<<i<<" "<<u<<' '<<v<<endl;
//cout<<"E:"<<E[i].u<<" "<<E[i].v<<" "<<E[i].flow<<endl;
//getchar();
int di=dfs(v,min(flow,E[i].flow));
if (di>0)
{
E[i].flow-=di;
E[i^1].flow+=di;
return di;
}
}
}
return 0;
}

void outp(int u)
{
setS[u]=1;