Time Limit: 3000MS | Memory Limit: 131072K | |
Total Submissions: 9890 | Accepted: 3067 |
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
#include<iostream> #include<cstdio> #include<queue> #include<cstring> using namespace std; struct node { int a,b,c; int next; }e[202005]; int fst[1005]; bool v[1005]; int m,n,a,b,c,num; int cnt[1005]; char p[5]; int dist[1005]; bool spfa()//spfa判断是否有解 { for(int i=0;i<=n;i++)dist[i]=1;//比0点到它的边的长度大即可 dist[0]=0; queue<int>q; q.push(0); cnt[0]++; v[0]=1; while(!q.empty()) { a=q.front(); q.pop(); for(int i=fst[a];i!=-1;i=e[i].next) { b=e[i].b; c=e[i].c; if(dist[b]>dist[a]+c) { dist[b]=dist[a]+c; if(!v[b]) { q.push(b); v[b]=1; cnt[b]++; if(cnt[b]>n)return false;//说明有负环路,无解退出 } } } v[a]=0; } return true; } int main() { int ans; while(scanf("%d%d",&n,&m)!=EOF) { num=0; ans=0; memset(fst,-1,sizeof(fst)); memset(cnt,0,sizeof(cnt)); memset(v,0,sizeof(v)); for(int i=0;i<m;i++) { scanf("%s",&p); if(p[0]=='P') { scanf("%d%d%d",&a,&b,&c); if(a==b&&c)ans=1;//自己连自己的点特殊处理 e[num].next=fst[a];//等式变为俩不等式,即建立双向边,这里邻接表存储 fst[a]=num; e[num].a=a; e[num].b=b; e[num++].c=-c; e[num].next=fst[b]; fst[b]=num; e[num].a=b; e[num].b=a; e[num++].c=c; } else { scanf("%d%d",&a,&b); if(a==b)ans=1;//自己连自己的特殊处理 e[num].next=fst[a];//不等式只建立单向边 fst[a]=num; e[num].a=a; e[num].b=b; e[num++].c=-1; } } for(int i=1;i<=n;i++) { e[num].next=fst[0]; fst[0]=num; e[num].a=0; e[num].b=i; e[num++].c=0; } if(ans)cout<<"Unreliable"<<endl; else if(spfa())cout<<"Reliable"<<endl; else cout<<"Unreliable"<<endl; } return 0; }