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
从情报组织获得了一些准确情报p,和模糊情报v,p给出了两个点a,b之间的准确距离c,v只给出a到b的距离至少为1,并且所有点在一条直线上。求这些情报是否可靠。
对于p情报,不仅能获得正向的边权值,还能算出反向的负权,如果后面给出的情报不对,就会形成一条负权的环路,把第一个样例画一画就知道了。用spfa求是否有负权环路即可
#include <iostream>
#include <cstring>
#include <string>
#include <vector>
#include <queue>
#include <cstdio>
#include <set>
#include <math.h>
#include <algorithm>
#include <queue>
#include <iomanip>
#include <map>
#include <cctype>
#define INF 0x3f3f3f3f
#define MAXN 1000005
#define Mod 1000000007
using namespace std;
struct Edge
{
int v,w,next;
};
Edge edge1[MAXN<<1];
int head1[MAXN],n,m,e,vis[MAXN],dis[MAXN];
int p[MAXN];
void add(Edge *edge,int *head,int u,int v,int w)
{
edge[e].v=v;
edge[e].w=w;
edge[e].next=head[u];
head[u]=e;
e++;
}
bool spfa(Edge *edge,int *head,int u)
{
memset(vis,0,sizeof(vis));
for(int i=1;i<=n;++i)
dis[i]=-INF;
dis[u]=0;
queue<int> q;
q.push(u);
while(!q.empty())
{
u=q.front();
q.pop();
vis[u]=0;
p[u]++;
if(p[u]>n)
return false;
for(int i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].v,w=edge[i].w;
if(w+dis[u]>dis[v])
{
dis[v]=w+dis[u];
if(!vis[v])
{
vis[v]=1;
q.push(v);
}
}
}
}
return true;
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
e=0;
memset(head1,-1,sizeof(head1));
memset(p,0,sizeof(p));
for(int i=0;i<m;++i)
{
int a,b,c;
char op;
cin>>op;
if(op=='P')
{
scanf("%d%d%d",&a,&b,&c);
add(edge1,head1,a,b,c);
add(edge1,head1,b,a,-c);
}
else
{
scanf("%d%d",&a,&b);
add(edge1,head1,a,b,1);
}
}
for(int i=1;i<=n;++i)
add(edge1,head1,0,i,0);
if(spfa(edge1,head1,0))
printf("Reliable\n");
else
printf("Unreliable\n");
}
return 0;
}