您的位置:首页 > 产品设计 > UI/UE

ZOJ 2587 Unique Attack 最小割

2014-10-07 17:20 211 查看
Unique Attack

Time Limit: 5 Seconds Memory Limit: 32768 KB

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 input file consists of several cases. In each case, 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 a single line. 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
0 0 0 0


Sample Output

UNIQUE
AMBIGUOUS


判断最小割是否唯一,在残留网络中,若能遍历到所有点则唯一,否则不唯一

#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <bitset>

using namespace std;

#define PB push_back
#define MP make_pair
#define REP(i,n) for(int i=0;i<(n);++i)
#define FOR(i,l,h) for(int i=(l);i<=(h);++i)
#define DWN(i,h,l) for(int i=(h);i>=(l);--i)
#define CLR(vis,pos) memset(vis,pos,sizeof(vis))
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LINF 1000000000000000000LL
#define eps 1e-8

typedef long long ll;

const int mm=11111*4;
const int mn=888;

int n,m;
int node,s,t,edge,max_flow;

int ver[mm],flow[mm],next[mm];

int head[mn],work[mn],dis[mn],q[mn];

int vis[mn];

inline void init(int _node,int _s,int _t)
{
    node=_node, s=_s, t=_t;
    for(int i=0;i<node;++i)
        head[i]=-1;
    edge=max_flow=0;
}

inline void addedge(int u,int v,int c)
{
    ver[edge]=v,flow[edge]=c,next[edge]=head[u],head[u]=edge++;
    ver[edge]=u,flow[edge]=0,next[edge]=head[v],head[v]=edge++;
}

bool Dinic_bfs()
{
    int i,u,v,l,r=0;
    for(i=0;i<node;++i)  dis[i]=-1;
    dis[ q[r++]=s ] = 0;
    for(l=0;l<r;l++)
    {
       for(i=head[ u=q[l] ]; i>=0 ;i=next[i])
        if(flow[i] && dis[ v=ver[i] ]<0)
        {
            dis[ q[r++]=v ]=dis[u]+1;
            if(v==t) return 1;
        }
    }
    return 0;
}

int Dinic_dfs(int u,int exp)
{
    if(u==t) return exp;
    for(int &i=work[u],v,temp; i>=0 ;i=next[i])
    {
        if(flow[i] && dis[ v=ver[i] ]==dis[u]+1 && ( temp=Dinic_dfs(v,min(exp,flow[i])) )>0)
        {
           flow[i]-=temp;
           flow[i^1]+=temp;
           return temp;
        }
    }
    return 0;
}

int Dinic_flow()
{
    int res,i;
    while(Dinic_bfs())
    {
        for(i=0;i<node;++i) work[i]=head[i];
        while( ( res=Dinic_dfs(s,INF) ) )  max_flow+=res;
    }
    return  max_flow;
}

void dfs1(int u){
    vis[u]=1;
    for(int i=head[u]; ~i ; i=next[i]){
        if(!vis[ver[i]] && flow[i]){
            dfs1(ver[i]);
        }
    }
}

void dfs2(int u){
    vis[u]=1;
    for(int i=head[u]; ~i ; i=next[i]){
        if(!vis[ver[i]] && flow[i^1]){
            dfs2(ver[i]);
        }
    }
}

int main()
{
    int ss,tt;
    cin>>n>>m>>ss>>tt;
    ss--,tt--;
    init(n,ss,tt);
    int u,v,w;
    REP(i,m){
        scanf("%d%d%d",&u,&v,&w);
        u--,v--;
        addedge(u,v,w);
        addedge(v,u,w);
    }
    Dinic_flow();

    CLR(vis,0);
    dfs1(s);
    dfs2(t);
    int flag=0;
    REP(i,n){
        if(vis[i]==0){
            flag=1;
            break;
        }
    }

    if(flag==0) printf("UNIQUE\n");
    else     printf("AMBIGUOUS\n");

    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: