【最小割+判定唯一性】ZOJ-2587 Unique Attack

30 篇文章 0 订阅
26 篇文章 0 订阅
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
————————————————————愧疚的分割线————————————————————
前言:整整9天没有敲代码啊!!!
思路:这道题是判定最小割是否唯一的。关于如何判定最小割是否唯一,看到了一个非常好的证明——
判断最小割是否唯一,先求一次最大流,
然后在残留网络中分别从源汇开始dfs一次,
找出最小割[S,T],如果[S,T]不包含所有点,那么最小割不唯一。
假设点i不被[S,T]包含,
那么残留网络中 s 不能到达 i , i 不能到达 t ,即进入 i 的边和从 i 出去的边都满流,
假设某条进入 i 的边 x 满流,
这些流量从若干条边 y 流出 i ,那么,如果选 x 为割边,或者选所有对应的 y 为割边,
不会影响最大流,即最小割容量不变,最小割也就不唯一

另外这道题给出的是“光纤”,也就是说是无向图,不能确定正向弧。
这时候可以“添加两条有向边”——
add(u, v, c); add(v, u, 0);
add(v, u, c); add(u, v, 0);
由于同一条路上的流量可以合并,所以省去了0的部分。
代码如下:
/*
ID: j.sure.1
PROG:
LANG: C++
*/
/****************************************/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <string>
#include <climits>
#include <iostream>
#define LL long long
using namespace std;
const int INF = 0x3f3f3f3f;
/****************************************/
const int N = 888, M = 22222;
struct Node {
	int u, v, cap;
	int next;
}edge[M];
int n, m, S, T, tot, head[N], cur[N], lev[N], q[N], s[N];
bool vis1[N], vis2[N];
int ret1, ret2;

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

void add(int u, int v, int c)
{
	edge[tot].u = u; edge[tot].v = v; edge[tot].cap = c;
	edge[tot].next = head[u]; head[u] = tot++;
}

bool bfs()
{
	memset(lev, -1, sizeof(lev));
	int fron = 0, rear = 0;
	q[rear++] = S;
	lev[S] = 0;
	while(fron < rear) {
		int u = q[fron%N]; fron++;
		for(int i = head[u]; i != -1; i = edge[i].next) {
			int v = edge[i].v;
			if(edge[i].cap && lev[v] == -1) {
				lev[v] = lev[u] + 1;
				q[rear%N] = v; rear++;
				if(v == T) return true;
			}
		}
	}
	return false;
}

void Dinic()
{
	while(bfs()) {
		memcpy(cur, head, sizeof(head));
		int u = S, top = 0;
		while(1) {
			if(u == T) {
				int mini = INF, loc;
				for(int i = 0; i < top; i++) {
					if(edge[s[i]].cap < mini) {
						mini = edge[s[i]].cap;
						loc = i;
					}
				}
				for(int i = 0; i < top; i++) {
					edge[s[i]].cap -= mini;
					edge[s[i]^1].cap += mini;
				}
				top = loc;
				u = edge[s[top]].u;
			}
			int &i = cur[u];
			for(; i != -1; i = edge[i].next) {
				int v = edge[i].v;
				if(edge[i].cap && lev[v] == lev[u] + 1) break;
			}
			if(i != -1) {
				s[top++] = i;
				u = edge[i].v;
			}
			else {
				if(!top) break;
				lev[u] = -1;
				u = edge[s[--top]].u;
			}
		}
	}
}

void dfs1(int x)
{
	vis1[x] = 1;
	ret1++;
	for(int i = head[x]; ~i; i = edge[i].next) {
		int v = edge[i].v;
		if(!vis1[v] && edge[i].cap) dfs1(v);
	}
}

void dfs2(int x)
{
	vis2[x] = 1;
	ret2++;
	for(int i = head[x]; ~i; i = edge[i].next) {
		int v = edge[i].v;
		if(!vis2[v] && edge[i^1].cap) dfs2(v);
	}//注意,要判断的是正向弧
}

int main()
{
#ifdef J_Sure
//	freopen("000.in", "r", stdin);
//	freopen(".out", "w", stdout);
#endif
	while(~scanf("%d%d%d%d", &n, &m, &S, &T)) {
		if(!n && !m && !S && !T) break;
		int u, v, c;
		init();
		for(int j = 0; j < m; j++) {
			scanf("%d%d%d", &u, &v, &c);
			add(u, v, c); add(v, u, c);
		}
		Dinic();
		for(int i = 1; i <= n; i++) {
			vis1[i] = vis2[i] = 0;
		}
		ret1 = ret2 = 0;
		dfs1(S); dfs2(T);
		if(n == ret1 + ret2) puts("UNIQUE");
		else puts("AMBIGUOUS");
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值