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

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

2015-09-22 22:08 621 查看
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 asimple 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 fromone 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 computersof the network, so that there is no way to transmit data from one of them to another. For each fibrethe terrorists have calculated the sum of money they
need to destroy the fibre. Of course, they want tominimize the cost of the operation, so it is required that the total sum spent for destroying the fibres wasminimal 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 thatthe main supercomputers cannot connect to each
other after it and the cost of the operation is minimalpossible.

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,
andthe 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 connectsare given and the cost of destroying this connection. It is guaranteed that all costs are non-negativeinteger 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 mainsupercomputer 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

题意:



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

解析:

1、我们先对原图求一次最大流

2、对残留网络,我们从S开始,找到所有所有S能到达的点;再从T开始,找出所有能到达T的点。

3、判断原网络中是否还有没有访问到的点,如果没有,则唯一,否者,不唯一!





#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
#define maxn 1100
#define maxm 1100000
#define INF 0x3f3f3f3f

int n, m, s, t;
int head[maxn], cur[maxn], cnt;
int dist[maxn], vis[maxn];
int num;

struct node {
    int u, v, cap, flow, next;
};
node edge[maxm];

void init(){
    cnt = 0;
    memset(head, -1, sizeof(head));
}

void add(int u, int v, int w){
    edge[cnt] = {u, v, w, 0, head[u]};
    head[u] = cnt++;
    edge[cnt] = {v, u, 0, 0, head[v]};
    head[v] = cnt++;
}

void getmap(){
    int a, b, c;
    while(m--){
        scanf("%d%d%d", &a, &b, &c);
        add(a, b, c);
        add(b, a, c);
    }
}

bool BFS(int st ,int ed){
    queue <int> q;
    memset(vis, 0, sizeof(vis));
    memset(dist, -1, sizeof(dist));
    dist[st] = 0;
    vis[st] = 1;
    q.push(st);
    while(!q.empty()){
        int u = q.front();
        q.pop();
        for(int i = head[u]; i != -1; i = edge[i].next){
            node E = edge[i];
            if(!vis[E.v] && E.cap > E.flow){
                vis[E.v] = 1;
                dist[E.v] = dist[u] + 1;
                if(E.v == ed) return true;
                q.push(E.v);
            }
        }
    }
    return false;
}

int DFS(int x, int ed, int a){
    if(x == ed || a == 0)
        return a;
    int flow = 0, f;
    for(int &i = cur[x]; i != -1; i = edge[i].next){
        node &E = edge[i];
        if(dist[E.v] == dist[x] + 1 && (f = DFS(E.v, ed, min(a, E.cap - E.flow))) > 0){
            E.flow += f;
            edge[i ^ 1].flow -= f;
            a -= f;
            flow += f;
            if(a == 0) break;
        }
    }
    return flow;
}

void maxflow(int st, int ed){
    while(BFS(st, ed)){
        memcpy(cur, head, sizeof(head));
        DFS(st, ed, INF);
    }
}

void dfs1(int u){//从S开始,找到所有所有S能到达的点
    vis[u] = 1;
    for(int i = head[u]; i != -1; i = edge[i].next){
        node E = edge[i];
        if(!vis[E.v] && E.cap > E.flow)
            dfs1(E.v);
    }
}

void dfs2(int u){//从T开始,找出所有能到达T的点
    vis[u] = 1;
    for(int i = head[u]; i != -1; i = edge[i].next){
        node E = edge[i];
        if(!vis[E.v] && edge[i ^ 1].cap > edge[i ^ 1].flow)
            dfs2(E.v);
    }
}

int main (){
    while(scanf("%d%d%d%d", &n, &m, &s, &t), n || m || s || t){
        init();
        getmap();
        maxflow(s, t);
        int flag  = 0;
        memset(vis, 0, sizeof(vis));
        vis[s] = vis[t] = 1;
        num = 0;
        dfs1(s); dfs2(t);
        for(int i = 1; i <= n; ++i)
            if(!vis[i]){
                flag = 1;
                break;
            }
        if(flag == 1)
            printf("AMBIGUOUS\n");
        else
            printf("UNIQUE\n");//不唯一
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: