差分约束系统——POJ 2983

对应POJ题目:点击打开链接


Is the Information Reliable?
Time Limit: 3000MS   Memory Limit: 131072K
Total Submissions: 11393   Accepted: 3594

Description

The galaxy war between the Empire Draco and the Commonwealth of Zibu broke out 3 years ago. Draco established a line of defense called Grot. Grot is a straight line with N defense stations. Because of the cooperation of the stations, Zibu’s Marine Glory cannot march any further but stay outside the line.

A mystery Information Group X benefits form selling information to both sides of the war. Today you the administrator of Zibu’s Intelligence Department got a piece of information about Grot’s defense stations’ arrangement from Information Group X. Your task is to determine whether the information is reliable.

The information consists of M tips. Each tip is either precise or vague.

Precise tip is in the form of P A B X, means defense station A is X light-years north of defense station B.

Vague tip is in the form of V A B, means defense station A is in the north of defense station B, at least 1 light-year, but the precise distance is unknown.

Input

There are several test cases in the input. Each test case starts with two integers N (0 < N ≤ 1000) and M (1 ≤ M ≤ 100000).The next M line each describe a tip, either in precise form or vague form.

Output

Output one line for each test case in the input. Output “Reliable” if It is possible to arrange N defense stations satisfying all the M tips, otherwise output “Unreliable”.

Sample Input

3 4
P 1 2 1
P 2 3 1
V 1 3
P 1 3 1
5 5
V 1 2
V 2 3
V 3 4
V 4 5
V 3 5

Sample Output

Unreliable
Reliable

Source

题意:给出两种关于防御站位置的信息,一种是确切的信息,P A B X,表示A在B北面x距离的地方,另一种是V A B,表示只知道A在B的北面。问这些信息有没有矛盾。

思路:确切信息可表示为  A - B >= X && A - B <= X,模糊信息可表示为 A - B >= 1

即是分别表示为:A - B <= X && B - A <= -X 和 B - A <= -1

建图用Bellman_Ford或Spfa都可以解决,其中前者可直接解决,后者需要加入一个超级源,使到每个点的距离为0(这样才能使每个点都走过,否则对于不连通的图,这不能)。

Spfa:


#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<map>
#include<queue>
#include<stack>
#include<vector>
#include<algorithm>
#include<cstring>
#include<string>
#include<iostream>
const int MAXN=1000+10;
const int INF=1<<30;
using namespace std;
int dis[MAXN];
int in[MAXN];
int vis[MAXN];
int head[MAXN];
int size,n,m;

struct Edge
{
	int v,w,next;
}E[202000];

void Build(int u, int v, int w)
{
	E[size].v = v;
	E[size].w = w;
	E[size].next = head[u];
	head[u] = size++;
}

int Spfa()
{
	memset(vis,0,sizeof(vis));
	memset(in,0,sizeof(in));
	for(int i=0; i<=n; i++) dis[i] = INF;
	dis[0] = 0;
	queue<int>q;
	q.push(0);
	vis[0] = 1;
	while(!q.empty())
	{
		int u = q.front();
		vis[u] = 0;
		q.pop();
		for(int i=head[u]; i!=-1; i=E[i].next){
			int v = E[i].v;
			int w = E[i].w;
			if(dis[u] + w < dis[v]){
				dis[v] = dis[u] + w;
				if(!vis[v]){
					vis[v] = 1;
					in[v]++;
					if(in[v] >= n) return 0;
					q.push(v);
				}
			}
		}
	}
	return 1;
}

int main()
{
	//freopen("in.txt","r",stdin);
	while(~scanf("%d%d", &n,&m))
	{
		memset(head,-1,sizeof(head));
		size = 0;
		char ch[3];
		int a,b,w;
		for(int i=0; i<m; i++){
			scanf("%s", ch);
			if(ch[0] =='P'){
				scanf("%d%d%d", &a,&b,&w);
				Build(a,b,w);
				Build(b,a,-w);
			}
			else{
				scanf("%d%d", &a,&b);
				Build(b,a,-1);
			}
		}
		for(int i=1; i<=n; i++)
			Build(0,i,0);//加入超级源
		int ok = Spfa();
		if(ok) printf("Reliable\n");
		else printf("Unreliable\n");
	}
	return 0;
}

Bellman_Ford:


#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<map>
#include<queue>
#include<stack>
#include<vector>
#include<algorithm>
#include<cstring>
#include<string>
#include<iostream>
const int MAXN=1000+10;
const int INF=1<<30;
using namespace std;
int dis[MAXN];
int size,n,m;

struct Edge
{
	int u,v,w;
}E[200000];

void Build(int u, int v, int w)
{
	E[size].u = u;
	E[size].v = v;
	E[size++].w = w;
}

int Bellman_Ford()
{
	for(int i=1; i<=n; i++) dis[i] = INF;
	dis[1] = 0;
	for(int i=1; i<=n-1; i++){
		for(int j=0; j<size; j++){
			if(dis[E[j].u] + E[j].w < dis[E[j].v])
				dis[E[j].v] = dis[E[j].u] + E[j].w;
		}
	}
	for(int i=0; i<size; i++)
		if(dis[E[i].u] + E[i].w < dis[E[i].v]) return 0;
	return 1;
}

int main()
{
	//freopen("in.txt","r",stdin);
	while(~scanf("%d%d", &n,&m))
	{
		size = 0;
		char ch[3];
		int a,b,w;
		for(int i=0; i<m; i++){
			scanf("%s", ch);
			if(ch[0] =='P'){
				scanf("%d%d%d", &a,&b,&w);
				Build(a,b,w);
				Build(b,a,-w);
			}
			else{
				scanf("%d%d", &a,&b);
				Build(b,a,-1);
			}
		}
		int ok = Bellman_Ford();
		if(ok) printf("Reliable\n");
		else printf("Unreliable\n");
	}
	return 0;
}





你可能感兴趣的:(差分约束系统——POJ 2983)