UVA 10480 Sabotage 【最大流最小割】

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.

扫描二维码关注公众号,回复: 4401248 查看本文章

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

最大流最小割问题,要求怎样切断边才能使花费最小并且减少的流量最大,然后注意怎么输出,理解EK的算法的细节

#include<cstring>
#include<string>
#include<cstdio>
#include<stdlib.h>
#include<iostream>
#include<algorithm>
#include<math.h>
#include<map>
#include<vector>
#include<stack>
#define inf 0x3f3f3f3f
#include<queue>
#include<set>
using namespace std;
typedef long long ll;
const int N=205;
const int M=2005;

struct node
{
    int u,v,f,ne;
}edge[M];

int head[N],a[N];//a[]数组存每次bfs后残留网络
int pre[N];
int s,t,e;

void add_insert(int a,int b,int c)//邻接表读入
{
    edge[e].u=a;
    edge[e].v=b;
    edge[e].f=c;
    edge[e].ne=head[a];
    head[a]=e++;
}

void add(int a,int b,int c)
{
    add_insert(a,b,c);
    add_insert(b,a,c);//注意反向边的容量也是c
}

void init()
{
    memset(head,-1,sizeof(head));
    e=0;s=1;t=2;
}

void solve()
{
    int ans=0;
    queue<int>que;
    while(true)
    {
        que.push(s);
        memset(a,0,sizeof(a));
        memset(pre,-1,sizeof(pre));
        a[s]=inf;
        while(!que.empty())
        {
            int u=que.front();
            que.pop();
            for(int i=head[u];i!=-1;i=edge[i].ne)
            {
                int v=edge[i].v,f=edge[i].f;
                if(!a[v]&&f)
                {
                    pre[v]=i;
                    que.push(v);
                    a[v]=min(a[u],f);
                }
            }
        }
        if(!a[t]) break;
        for(int v=pre[t];v!=-1;v=pre[edge[v^1].v])
        {
            edge[v].f-=a[t];
            edge[v^1].f+=a[t];
        }
    }
}

int main()
{
    int n,m;
    while(~scanf("%d %d",&n,&m))
    {
        if(!n&&!m) break;
        int u,v,w;
        init();
        for(int i=0;i<m;i++)
        {
            scanf("%d %d %d",&u,&v,&w);
            add(u,v,w);
        }
        solve();
        for(int i=0;i<e;i+=2)
        {   //进行完EK以后,剩下的就是残留网路,只要找到出现断边的就输出
            if((!a[edge[i].u]&&a[edge[i].v])||(a[edge[i].u]&&!a[edge[i].v]))
                printf("%d %d\n",edge[i].u,edge[i].v);
        }
        printf("\n");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41984014/article/details/83831868