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

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;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-10 19:01:18

zoj 2587 Unique Attack 【判断最小割是否唯一】的相关文章

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

很裸的判断最小割是否唯一.判断方法是先做一遍最大流求最小割,然后从源点和汇点分别遍历所有能够到达的点,看是否覆盖了所有的点,如果覆盖了所有的点,那就是唯一的,否则就是不唯一的. #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #include <iostr

ZOJ 2587 Unique Attack(最小割唯一性判断)

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2587 题意:判断最小割是否唯一. 思路: 最小割唯一性的判断是先跑一遍最大流,然后在残留网络中分别从源点和汇点出发dfs,只有当该边还有流量可用时可以访问下一个顶点,最后如果所有顶点都访问了,那么就是唯一的,否则不唯一. 接下来图解一下: 先看下面这个容量均为1的图: 跑一遍最大流后的残留网络如下(只画正向弧): 接下来从源点和汇点出发都无法访问任何顶点,因为剩余流量皆为

zoj2587 Unique Attack 判断最小割是否唯一

题目链接: zoj2587 题意: 给出一张无向网络图,并给出起点和终点,破坏图的每一条边需要一定的费用,问破坏起点和终点的连通性的费用是否唯一. 解题思路: 破坏两点的连通性的最小费用,很容易联想到 网络流中的最小割, 建立源点 汇点 同时 因为图是无向图,我们需要将每条边建两次(正反向). 然后就是判断这个最小割是否唯一了: 首先 从源点开始 dfs  通过非饱和边  统计所有能走到的点  记为s1 然后 从汇点开始 dfs  通过非饱和边  统计所有能走到的点 记为s2 如果s1+s2==

ZOJ 2587 Unique Attack (判断最小割的唯一性)

ZOJ 2587 Unique Attack 链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1587 题意:N 台超级计算机连成一个网络.M 对计算机之间用光纤直接连在一起,光纤的连接是双向的.数据可以直接在有光纤直接连接的计算机之间传输,也可以通过一些计算机作为中转来传输. 有一群恐怖分子计划攻击网络.他们的目标是将网络中两台主计算机断开,这样这两台计算机之间就无法传输数据了.恐怖分子已经计算好了摧毁每条光纤所需要花

zoj 2587 Unique Attack 最小割判定

题目链接 让你判断最小割是否唯一. 判断方法是, 先求一遍最大流, 然后从源点dfs一次, 搜索未饱和边的数目. 从汇点dfs一次, 同样也是搜索未饱和边的数目, 看总和是否等于n. 如果等于n那么唯一. 具体可以看这里, http://www.cnblogs.com/Lyush/archive/2013/05/01/3053640.html. 1 #include<bits/stdc++.h> 2 using namespace std; 3 #define mem(a) memset(a,

zoj 3792 Romantic Value(最小割下边数最小)

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5300 大致题意:给出一个无向图,以及起点与终点.要删除一些边使得起点与终点不连通,在删掉边的权值之和最小的情况下要求删除的边数尽量少.求出一个比值:剩余边数权值和/删除的边数. 思路:删除边的权值之和最小显然是求最小割即最大流.但同时要求删除边数最少,解决方法是把边数也加到权值上去,一起求最大流,因为边数最多是1000,即每条边的边权置为 w*10000+1,1代表这一条边.

zoj 2874 &amp; poj 3308 Paratroopers (最小割)

题意: 一个m*n大小的网格,已知伞兵着陆的具体位置(行和列).现在在某行(或某列) 安装一架激光枪,一架激光枪能杀死该行(或该列)所有的伞兵.在第i行安装一架 激光枪的费用是Ri,在第i列安装的费用是Ci.要安装整个激光枪系统,总费用为这些 激光枪费用的乘积. 求杀死所有伞兵的最小费用. 构图: 把伞兵视为边,行与列视为顶点.增加源点和汇点,对于第i行,从源点向顶点i连接一条 容量为Ri的边.对于第j列,从顶点j向汇点连接一条容量为Rj的边. 如果某一点(i,j)有伞兵降落,则从顶点Ri向顶点

zoj 2587 判断最小割的唯一性

算法: 先求出残量网络,计算出从src能够到的点集A,再求出能够到dst的点集B,如果所有点都被访问到了,那么割就是唯一的,即(A,B),否则(A,V-A)和(V-B,B)都是最小割. (注意因为割的本质是有向边集,而不是点集V的划分,所以(A,V-A)和(V-B,B)有可能本质上还是同一个最小割,比如随便再加一个孤立点,虽然割还是唯一的,但还是有点没有被访问到,所以我们限制原图中所有点要么可以从src到达,要么可以到达dst) 1 #include <cstdio> 2 #include &

ZOJ2587 Unique Attack(判定最小割唯一性)

看了题解,自己大概想了下. 最小割唯一的充分必要条件是残量网络中所有点要嘛能从源点floodfill到要嘛能floodfill到汇点. 必要性,这是当然的,因为假设从源点floodfill或者从汇点反着floodfill得到的集合若不相补,那这就有两个最小割的方案,最小割不唯一. 充分性,首先这样就找到一个最小割,它在两次floodfill的交界处,假设还存在另一个最小割在靠近源点或者靠近汇点处那必然floodfill时找到的是它,这与另一个最小割矛盾,所以仅存在这么一个在交界处的最小割. 于是