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
POJ Monthly–2006.08.27, Dagger
大致题意:给出一些点的精确信息和模糊信息,精确信息给出两点的位置和距离,模糊信息给出两点的位置,但距离大于等于一。试确定是否所有的信息满足条件。
分析:
思路:
由P 可以得到 v - u = w
也就是u - v <= w && v - u <= -w ,
由 V u v 得到 v - u >= 1
也就是 u - v <= -1 ;
建图,使用最短路,判断是否会有负环。
代码:
const
MaxE=102;
MaxV=500001;
type
rec=record
x,y,w,next:longint;
end;
var
n,m,c,qx,i,x,y,w,o,q,p:longint;
g:array [-1..Maxv] of rec;
ls:array [-1..Maxe] of longint;
v,d,list,sum:array [-1..maxe] of longint;
ch:string;
boo:boolean;
procedure spfa(first:longint);
var
head,tail,t,i,qe:longint;
begin
tail:=1;
list[1]:=first;
for i:=1 to n do
d[i]:=maxlongint;
d[first]:=0;
v[first]:=1;
while tail<>0 do
begin
t:=ls[list[tail]];
qe:=list[tail];
tail:=tail-1;
while t>0 do
with g[t] do
begin
if d[x]+w<d[y] then
begin
d[y]:=d[x]+w;
if v[y]=0 then
begin
v[y]:=1;
tail:=tail+1;
list[tail]:=y;
inc(sum[y]);
if (sum[y]>=n) then
begin
boo:=false;
exit;
end;
end;
end;
t:=next;
end;
v[qe]:=0;
end;
end;
procedure add(x,y,w:longint);
begin
inc(o);
g[o].x:=x;
g[o].y:=y;
g[o].w:=w;
g[o].next:=ls[x];
ls[x]:=o;
end;
begin
read(n);
while n>0 do
begin
readln(m);
for i:=1 to m do
begin
x:=0; y:=0; w:=0;
readln(ch);
p:=pos(' ',ch);
val(copy(ch,1,p-1),x);
delete(ch,1,p);
p:=pos(' ',ch);
val(copy(ch,1,p-1),y);
delete(ch,1,p);
p:=pos(' ',ch);
val(copy(ch,p+1,length(ch)),w);
if ch[1]='g' then add(x-1,x+y,-w-1)
else add(x+y,x-1,w-1);
end;
for i:=0 to n do
add(-1,i,0);
boo:=true;
spfa(-1);
fillchar(g,sizeof(g),0);
fillchar(ls,sizeof(ls),0);
o:=0;
fillchar(v,sizeof(v),0);
fillchar(d,sizeof(d),$7f);
fillchar(sum,sizeof(sum),0);
fillchar(list,sizeof(list),0);
if boo then writeln('lamentable kingdom')
else writeln('successful conspiracy');
read(n);
end;
end.