差分约束系统——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;
}





Python网络爬虫与推荐算法新闻推荐平台:网络爬虫:通过Python实现新浪新闻的爬取,可爬取新闻页面上的标题、文本、图片、视频链接(保留排版) 推荐算法:权重衰减+标签推荐+区域推荐+热点推荐.zip项目工程资源经过严格测试可直接运行成功且功能正常的情况才上传,可轻松复刻,拿到资料包后可轻松复现出一样的项目,本人系统开发经验充足(全领域),有任何使用问题欢迎随时与我联系,我会及时为您解惑,提供帮助。 【资源内容】:包含完整源码+工程文件+说明(如有)等。答辩评审平均分达到96分,放心下载使用!可轻松复现,设计报告也可借鉴此项目,该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的。 【提供帮助】:有任何使用问题欢迎随时与我联系,我会及时解答解惑,提供帮助 【附带帮助】:若还需要相关开发工具、学习资料等,我会提供帮助,提供资料,鼓励学习进步 【项目价值】:可用在相关项目设计中,皆可应用在项目、毕业设计、课程设计、期末/期中/大作业、工程实训、大创等学科竞赛比赛、初期项目立项、学习/练手等方面,可借鉴此优质项目实现复刻,设计报告也可借鉴此项目,也可基于此项目来扩展开发出更多功能 下载后请首先打开README文件(如有),项目工程可直接复现复刻,如果基础还行,也可在此程序基础上进行修改,以实现其它功能。供开源学习/技术交流/学习参考,勿用于商业用途。质量优质,放心下载使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值