ZOJ 2587 && ACdream1235 Unique Attack(判断最小割是否唯一)

87 篇文章 0 订阅

Unique Attack

Time Limit: 2000/1000MS (Java/Others)  Memory Limit: 128000/64000KB (Java/Others)
Problem Description

      N supercomputers in the United States of Antarctica are connected into a network. A network has a simple topology: M different pairs of supercomputers are connected to each other by an optical fibre. All connections are two-way, that is, they can be used in both directions. Data can be transmitted from one computer to another either directly by a fibre, or using some intermediate computers.

      A group of terrorists is planning to attack the network. Their goal is to separate two main computers of the network, so that there is no way to transmit data from one of them to another. For each fibre the terrorists have calculated the sum of money they need to destroy the fibre. Of course, they want to minimize the cost of the operation, so it is required that the total sum spent for destroying the fibres was minimal possible.

      Now the leaders of the group wonder whether there is only one way to do the selected operation. That is, they want to know if there are no two different sets of fibre connections that can be destroyed, such that the main supercomputers cannot connect to each other after it and the cost of the operation is minimal possible.

Input

      The first line of the input file contains N, M, A and B (2 <= N <= 800, 1 <= M <= 10000, 1 <= A,B <= N, A != B), specifying the number of supercomputers in the network, the number of fibre connections, and the numbers of the main supercomputers respectively. A case with 4 zeros indicates the end of file.

      Next M lines describe fibre connections. For each connection the numbers of the computers it connects are given and the cost of destroying this connection. It is guaranteed that all costs are non-negative integer numbers not exceeding 105, no two computers are directly connected by more than one fibre, no fibre connects a computer to itself and initially there is the way to transmit data from one main supercomputer to another.

Output
      If there is only one way to perform the operation, output “UNIQUE”. In the other case output “AMBIGUOUS”.
Sample Input
4 4 1 2
1 2 1
2 4 2
1 3 2
3 4 1
4 4 1 2
1 2 1
2 4 1
1 3 2
3 4 1
Sample Output
UNIQUE
AMBIGUOUS
Hint
Source
Andrew Stankevich Contest 4

题目大意:

    给一个图,以及起点终点,判断最小割是否唯一。


解题思路:

    首先从最小割的定义可以知道,最小割就是一个边集,将顶点分成两个部分。所以比较容易想到,当最小割不唯一的时候,在残余网络上存在一些顶点从s无法到达,也无法到达t。因此,我们只需要先跑一遍最大流,然后在残余网络上dfs统计s可达的顶点数和可达t的顶点数,如果和不为总顶点数则最小割不唯一,否则唯一。


AC代码:

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <cstdlib>
#include <cmath>
#include <map>
#include <string>
using namespace std;
#define INF 0x3f3f3f3f
#define LL long long
#define fi first
#define se second
#define mem(a,b) memset((a),(b),sizeof(a))

const int MAXV=800+3;

struct Edge
{
    int to,cap,rev;
    Edge(int t,int c,int r):to(t),cap(c),rev(r){}
};

vector<Edge> G[MAXV],rG[MAXV];
int level[MAXV];
int iter[MAXV];
int N,M,A,B;
bool vis[MAXV];

void add_edge(int from,int to,int cap)//添加无向边
{
    G[from].push_back(Edge(to,cap,G[to].size()));
    G[to].push_back(Edge(from,cap,G[from].size()-1));
}

void bfs(int s)
{
    mem(level,-1);
    queue<int> que;
    level[s]=0;
    que.push(s);
    while(!que.empty())
    {
        int v=que.front(); que.pop();
        for(int i=0;i<G[v].size();++i)
        {
            Edge &e=G[v][i];
            if(e.cap>0&&level[e.to]<0)
            {
                level[e.to]=level[v]+1;
                que.push(e.to);
            }
        }
    }
}

int dfs(int v,int t,int f)
{
    if(v==t)
        return f;
    for(int &i=iter[v];i<G[v].size();++i)
    {
        Edge &e=G[v][i];
        if(e.cap>0&&level[v]<level[e.to])
        {
            int d=dfs(e.to,t,min(f,e.cap));
            if(d>0)
            {
                e.cap-=d;
                G[e.to][e.rev].cap+=d;
                return d;
            }
        }
    }
    return 0;
}

int max_flow(int s,int t)
{
    int flow=0;
    for(;;)
    {
        bfs(s);
        if(level[t]<0)
            return flow;
        mem(iter,0);
        int f;
        while((f=dfs(s,t,INF))>0)
            flow+=f;
    }
}

int dfs1(int u)
{
    int num=1;
    for(int i=0;i<G[u].size();++i)
    {
        int v=G[u][i].to;
        if(G[u][i].cap>0&&!vis[v])
        {
            vis[v]=true;
            num+=dfs1(v);
        }
    }
    return num;
}

int dfs2(int u)
{
    int num=1;
    for(int i=0;i<rG[u].size();++i)
    {
        int v=rG[u][i].to;
        if(rG[u][i].cap>0&&!vis[v])
        {
            vis[v]=true;
            num+=dfs2(v);
        }
    }
    return num;
}

int main()
{
    while(~scanf("%d%d%d%d",&N,&M,&A,&B)&&(N||M||A||B))
    {
        for(int i=1;i<=N;++i)//初始化
        {
            G[i].clear();
            rG[i].clear();
        }
        for(int i=0;i<M;++i)
        {
            int a,b,c;
            scanf("%d%d%d",&a,&b,&c);
            add_edge(a,b,c);
        }
        max_flow(A,B);
        for(int i=1;i<=N;++i)//反向图
            for(int j=0;j<G[i].size();++j)
                rG[G[i][j].to].push_back(Edge(i,G[i][j].cap,0));//不需要用rev
        mem(vis,0);
        vis[A]=true;
        int num1=dfs1(A);
        mem(vis,0);
        vis[B]=true;
        int num2=dfs2(B);
        puts(num1+num2==N?"UNIQUE":"AMBIGUOUS");
    }
    
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值