Is the Information Reliable?
Time Limit: 3000MS | Memory Limit: 131072K | |
Total Submissions: 15783 | Accepted: 4893 |
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
题意:银河系中有n个防御站排成一条直线,给定m条信息,每条信息的格式如下。 P A B X代表A站在B站北方X光年处。 V A B 代表A站在B站北方,而且距离至少为1光年。 问是否存在这样的一个防御战排列满足上述要求,是输出Reliable,否输出 Unreliable。
从P A B X得到的差分系统为
dist[A] - dist[B] >= X && dist[A] - dist[B] <= X
等价于
dist[B] <= dist[A] - X && dist[A] <= dist[B] + X
从V A B得到的差分系统为
dist[A] >= dist[B] +1
等价于
dist[B] <= dist[A] -1
#include<iostream>
#include<cstdio>
#include<cmath>
#include<queue>
#include<cstring>
using namespace std;
const int N=1e5+10;
const int inf=0x3f3f3f3f;
struct node
{
int to,dis,next;
}edge[N<<2];
int cnt,head[N];
void addedge(int from,int to,int dis)
{
edge[cnt].to=to;
edge[cnt].dis=dis;
edge[cnt].next=head[from];
head[from]=cnt++;
}
int dis[N];
bool vis[N];
int num[N];
bool spfa(int n)
{
memset(num,0,sizeof(num));
memset(vis,false,sizeof(vis));
memset(dis,inf,sizeof(dis));
dis[0]=0;
vis[0]=true;
queue<int>Q;
Q.push(0);
while(!Q.empty()){
int u=Q.front();
Q.pop();
vis[u]=false;
//cout<<"u="<<u<<endl;
for(int i=head[u];i!=-1;i=edge[i].next){
int to=edge[i].to;
if(dis[to]>dis[u]+edge[i].dis){
dis[to]=dis[u]+edge[i].dis;
if(!vis[to]){
vis[to]=true;
Q.push(to);
if(++num[to]>n) return false;
}
}
}
}
return true;
}
int main()
{
int n,m;
while(scanf("%d %d",&n,&m)==2){
memset(head,-1,sizeof(head));
cnt=0;
for(int i=0;i<m;i++){
char t[10];
int a,b;
scanf("%s %d %d",t,&a,&b);
if(t[0]=='P'){
int c;
scanf("%d",&c);
addedge(a,b,-c);
addedge(b,a,c);
}
else{
addedge(a,b,-1);
}
}
for(int i=1;i<=n;i++){
addedge(0,i,0);
}
if(spfa(n)){
puts("Reliable");
}
else{
puts("Unreliable");
}
}
return 0;
}