现在的位置: 首页 > 综合 > 正文

ZOJ2587 Unique Attack

2017年10月18日 ⁄ 综合 ⁄ 共 4038字 ⁄ 字号 评论关闭

最小割判断唯一

先跑最大流,然后分别对源点和汇点DFS,记录能到达的点.

如果所有的点都能到达则最小割唯一,否则最小割有多解


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




Author: Andrew Stankevich
Source: Andrew Stankevich's Contest #5

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

#define Codeforces freopen("attack.in","r",stdin); freopen("attack.out","w",stdout);

using namespace std;

//isap 最大流

const int maxn=2000;
const int maxm=30000;
const int INF=0x3f3f3f3f;

struct Edge
{
    int to,next,cap,flow;
}edge[maxm];

int Size,Adj[maxn];
int gap[maxn],dep[maxn],pre[maxn],cur[maxn];

void init()
{
    Size=0;
    memset(Adj,-1,sizeof(Adj));
}

void addedge(int u,int v,int w,int rw=0) //单向边3个参数双向边4个
{
    edge[Size].to=v; edge[Size].cap=w; edge[Size].next=Adj[u];
    edge[Size].flow=0; Adj[u]=Size++;
    edge[Size].to=u; edge[Size].cap=rw; edge[Size].next=Adj[v];
    edge[Size].flow=0; Adj[v]=Size++;
}

int sap(int start,int end,int N) //源点 汇点 点的个数
{
    memset(gap,0,sizeof(gap));
    memset(dep,0,sizeof(dep));
    memcpy(cur,Adj,sizeof(Adj));

    int u=start;
    pre[u]=-1; gap[0]=N;
    int ans=0;

    while(dep[start]<N)
    {
        if(u==end)
        {
            int Min=INF;
            for(int i=pre[u];~i;i=pre[edge[i^1].to])
                if(Min>edge[i].cap-edge[i].flow)
                    Min=edge[i].cap-edge[i].flow;
            for(int i=pre[u];~i;i=pre[edge[i^1].to])
            {
                edge[i].flow+=Min;
                edge[i^1].flow-=Min;
            }
            u=start;
            ans+=Min;
            continue;
        }
        bool flag=false;
        int v;
        for(int i=cur[u];~i;i=edge[i].next)
        {
            v=edge[i].to;
            if(edge[i].cap-edge[i].flow&&dep[v]+1==dep[u])
            {
                flag=true;
                cur[u]=pre[v]=i;
                break;
            }
        }
        if(flag)
        {
            u=v;
            continue;
        }
        int Min=N;
        for(int i=Adj[u];~i;i=edge[i].next)
            if(edge[i].cap-edge[i].flow&&dep[edge[i].to]<Min)
            {
                Min=dep[edge[i].to];
                cur[u]=i;
            }
        gap[dep[u]]--;
        if(!gap[dep[u]]) return ans;
        dep[u]=Min+1;
        gap[dep[u]]++;
        if(u!=start) u=edge[pre[u]^1].to;
    }
    return ans;
}

bool vis[2][maxn];

void dfs(int u,int kind)
{
	vis[kind][u]=true;
	for(int i=Adj[u];~i;i=edge[i].next)
	{
		int v=edge[i].to;
		if(vis[kind][v]) continue;
		if(kind==0)
		{
			if(edge[i].flow<edge[i].cap) dfs(v,kind);
		}
		else if(kind==1)
		{
			if(-edge[i].flow<edge[i].cap) dfs(v,kind);
		}
	}
}

int n,m,s,t;

int main()
{
	//Codeforces
	while(scanf("%d%d%d%d",&n,&m,&s,&t)!=EOF)
	{
		if(n==0&&m==0&&s==0&&t==0) break;
		init();
		for(int i=0;i<m;i++)
		{
			int u,v,w;
			scanf("%d%d%d",&u,&v,&w);
			addedge(u,v,w,w);
		}
		int Maxflow=sap(s,t,n);
		//cout<<"Maxflow: "<<Maxflow<<endl;
		memset(vis,0,sizeof(vis));
		dfs(s,0); dfs(t,1);
		int cnt=0;
		for(int i=1;i<=n;i++)
		{
			if(vis[0][i]||vis[1][i]) cnt++;
		}
		if(cnt==n) puts("UNIQUE");
		else puts("AMBIGUOUS");
	}
	return 0;
}

抱歉!评论已关闭.