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

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

2011-09-16 15:19 369 查看
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

解法1:

求一次最大流,对每一个割边,将其权值改为inf,再做一次最大流,如果等于初始的流值,说明最小割不唯一。

用时1150 ms



解法2:

先做一次最大流,最大流之后图分成了两部分,然后从分别从源点和汇点进行dfs,

标记遍历的点,看图中有没有点不能被搜到 ,都能被搜到则最小割唯一。



思路(转):判断最小割是否唯一,先求一次最大流,然后在残留网络中分别从源汇开始dfs一次,找出最小割[S,T],如果SUT不包含所有点,那么最小割不唯一。假设点i不被SUT包含,那么残留网络中s不能到达i,i不能到达t,即进入i的边和从i出去的边都满流,假设某条进入i的边x满流,这些流量从若干条边y流出i,那么,如果选x为割边,或者选所有对应的y为割边,不会影响最大流,即最小割容量不变,最小割也就不唯一。

用时130 ms



代码1:

#include<cstdio>
#include<cstring>
#define N 1005
#define M 20005
#define inf 999999999
#define min(a,b) ((a)<(b)?(a):(b))

int n,m,s,t,num,adj
,dis
,q
,f
,f1
;
struct edge
{
	int v,w,c,pre;
}e[M];
void insert(int u,int v,int w)
{
	e[num]=(edge){v,w,w,adj[u]};
	adj[u]=num++;
	e[num]=(edge){u,w,w,adj[v]};
	adj[v]=num++;
}
int bfs()
{
	int i,x,v,head=0,tail=0;
	memset(dis,0,sizeof(dis));
	dis[s]=1;
	q[tail++]=s;
	while(head<tail)
	{
		x=q[head++];
		for(i=adj[x];~i;i=e[i].pre)
			if(e[i].w&&!dis[v=e[i].v])
			{
				dis[v]=dis[x]+1;
				if(v==t)
					return 1;
				q[tail++]=v;
			}
	}
	return 0;
}
int dfs(int x,int limit)
{
	if(x==t)
		return limit;
	int i,v,tmp,cost=0;
	for(i=adj[x];~i&&cost<limit;i=e[i].pre)
		if(e[i].w&&dis[x]==dis[v=e[i].v]-1)
		{
			tmp=dfs(v,min(limit-cost,e[i].w));
			if(tmp)
			{
				e[i].w-=tmp;
				e[i^1].w+=tmp;
				cost+=tmp;
			}
			else
				dis[v]=-1;
		}
	return cost;
}
int Dinic()
{
	int ans=0;
	while(bfs())
		ans+=dfs(s,inf);
	return ans;
}
int cnt1,cnt2;
void dfs1(int x)
{
	f[x]=1;
	cnt1++;
	for(int i=adj[x];~i;i=e[i].pre)
		if(!f[e[i].v]&&e[i].w)
			dfs1(e[i].v);
}
void dfs2(int x)
{
	f1[x]=1;
	cnt2++;
	for(int i=adj[x];~i;i=e[i].pre)
		if(!f1[e[i].v]&&e[i^1].w)
			dfs2(e[i].v);
}
int main()
{
	while(~scanf("%d%d%d%d",&n,&m,&s,&t),n)
	{
		int i,j,k;
		num=0;
		memset(adj,-1,sizeof(adj));
		while(m--)
		{
			scanf("%d%d%d",&i,&j,&k);
			insert(i,j,k);
		}
		Dinic();
		memset(f,0,sizeof(f));
		memset(f1,0,sizeof(f1));
		cnt1=cnt2=0;
		dfs1(s);
		dfs2(t);
		puts(cnt1+cnt2==n?"UNIQUE":"AMBIGUOUS");
	}
}



代码2:

#include<cstdio>
#include<cstring>
#define N 1005
#define M 20005
#define inf 999999999
#define min(a,b) ((a)<(b)?(a):(b))

int n,m,s,t,num,adj
,dis
,q
,f
,f1
;
struct edge
{
	int v,w,c,pre;
}e[M];
void insert(int u,int v,int w)
{
	e[num]=(edge){v,w,w,adj[u]};
	adj[u]=num++;
	e[num]=(edge){u,w,w,adj[v]};
	adj[v]=num++;
}
int bfs()
{
	int i,x,v,head=0,tail=0;
	memset(dis,0,sizeof(dis));
	dis[s]=1;
	q[tail++]=s;
	while(head<tail)
	{
		x=q[head++];
		for(i=adj[x];~i;i=e[i].pre)
			if(e[i].w&&!dis[v=e[i].v])
			{
				dis[v]=dis[x]+1;
				if(v==t)
					return 1;
				q[tail++]=v;
			}
	}
	return 0;
}
int dfs(int x,int limit)
{
	if(x==t)
		return limit;
	int i,v,tmp,cost=0;
	for(i=adj[x];~i&&cost<limit;i=e[i].pre)
		if(e[i].w&&dis[x]==dis[v=e[i].v]-1)
		{
			tmp=dfs(v,min(limit-cost,e[i].w));
			if(tmp)
			{
				e[i].w-=tmp;
				e[i^1].w+=tmp;
				cost+=tmp;
			}
			else
				dis[v]=-1;
		}
	return cost;
}
int Dinic()
{
	int ans=0;
	while(bfs())
		ans+=dfs(s,inf);
	return ans;
}
int cnt1,cnt2;
void dfs1(int x)
{
	f[x]=1;
	cnt1++;
	for(int i=adj[x];~i;i=e[i].pre)
		if(!f[e[i].v]&&e[i].w)
			dfs1(e[i].v);
}
void dfs2(int x)
{
	f1[x]=1;
	cnt2++;
	for(int i=adj[x];~i;i=e[i].pre)
		if(!f1[e[i].v]&&e[i^1].w)
			dfs2(e[i].v);
}
int main()
{
	while(~scanf("%d%d%d%d",&n,&m,&s,&t),n)
	{
		int i,j,k;
		num=0;
		memset(adj,-1,sizeof(adj));
		while(m--)
		{
			scanf("%d%d%d",&i,&j,&k);
			insert(i,j,k);
		}
		Dinic();
		memset(f,0,sizeof(f));
		memset(f1,0,sizeof(f1));
		cnt1=cnt2=0;
		dfs1(s);
		dfs2(t);
		puts(cnt1+cnt2==n?"UNIQUE":"AMBIGUOUS");
	}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: