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

zoj 2587 Unique Attack 【判断最小割是否唯一】

2015-08-28 10:44 525 查看
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

题意:给你N个点、M条无向边以及边的权值,又给你源点A和汇点B。问你A到B的最小割是否唯一。

最小割判定是否唯一:

1,先跑一次最大流,得到残量网络;

2,在残量网络中沿着未满流的边查找,统计A能达到的点数sum1和能达到B的点数sum2;

3,若sum1 + sum2 = N - 2则说明最小割唯一,反之不唯一。

AC代码:

#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
#define MAXN 1010
#define MAXM 50000+10
#define INF 0x3f3f3f3f
using namespace std;
struct Edge
{
    int from, to, cap, flow, next;
};
Edge edge[MAXM];
int head[MAXN], cur[MAXN], edgenum;
int dist[MAXN];
bool vis[MAXN];
int N, M, A, B;
void init()
{
    edgenum = 0;
    memset(head, -1, sizeof(head));
}
void addEdge(int u, int v, int w)
{
    Edge E1 = {u, v, w, 0, head[u]};
    edge[edgenum] = E1;
    head[u] = edgenum++;
    Edge E2 = {v, u, 0, 0, head[v]};
    edge[edgenum] = E2;
    head[v] = edgenum++;
}
void getMap()
{
    int a, b, c;
    while(M--)
    {
        scanf("%d%d%d", &a, &b, &c);
        addEdge(a, b, c);
        addEdge(b, a, c);
    }
}
bool BFS(int s, int t)
{
    queue<int> Q;
    memset(dist, -1, sizeof(dist));
    memset(vis, false, sizeof(vis));
    dist[s] = 0;
    vis[s] = true;
    Q.push(s);
    while(!Q.empty())
    {
        int u = Q.front();
        Q.pop();
        for(int i = head[u]; i != -1; i = edge[i].next)
        {
            Edge E = edge[i];
            if(!vis[E.to] && E.cap > E.flow)
            {
                dist[E.to] = dist[u] + 1;
                if(E.to == t) return true;
                vis[E.to] = true;
                Q.push(E.to);
            }
        }
    }
    return false;
}
int DFS(int x, int a, int t)
{
    if(x == t || a == 0) return a;
    int flow = 0, f;
    for(int &i = cur[x]; i != -1; i = edge[i].next)
    {
        Edge &E = edge[i];
        if(dist[E.to] == dist[x] + 1 && (f = DFS(E.to, min(a, E.cap-E.flow), t)) > 0)
        {
            edge[i].flow += f;
            edge[i^1].flow -= f;
            flow += f;
            a -= f;
            if(a == 0) break;
        }
    }
    return flow;
}
void Maxflow(int s, int t)
{
    while(BFS(s, t))
    {
        memcpy(cur, head, sizeof(head));
        DFS(s, INF, t);
    }
}
int ans;//超级源点能到的点数 + 超级汇点能到的点数
void sinksum(int u)//计算源点能到的点数
{
    for(int i = head[u]; i != -1; i = edge[i].next)//正向边
    {
        Edge E = edge[i];
        if(vis[E.to])
            continue;
        if(E.cap > E.flow)
        {
            ans++;
            vis[E.to] = true;
            sinksum(E.to);
        }
    }
}
void sourcesum(int u)//计算能到汇点的点数
{
    for(int i = head[u]; i != -1; i = edge[i].next)
    {
        Edge E = edge[i];
        if(vis[E.to])
            continue;
        if(edge[i^1].cap - edge[i^1].flow)//看反向弧
        {
            ans++;
            vis[E.to] = true;
            sourcesum(E.to);
        }
    }
}
void solve()
{
    ans = 0;
    memset(vis, false, sizeof(vis));
    vis[A] = vis[B] = true;
    sinksum(A);
    sourcesum(B);
    if(ans == N - 2)
        printf("UNIQUE\n");//最小割唯一
    else
        printf("AMBIGUOUS\n");//不唯一
}
int main()
{
    while(scanf("%d%d%d%d", &N, &M, &A, &B), N||M||A||B)
    {
        init();
        getMap();
        Maxflow(A, B);//跑一次最大流
        solve();//判断最小割是否唯一
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: