Is the Information Reliable?
Time Limit: 3000MS | Memory Limit: 131072K | |
Total Submissions: 14898 | Accepted: 4639 |
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
注意判一下自环
#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
int n, m, cnt;
const int MAX = 100000;
const int INF = 0x3f3f3f3f;
int head[MAX], dis[MAX], vis[MAX], t[MAX];
struct node{
int to, c, next;
node() {}
node(int to, int c, int next) : to(to), c(c), next(next) {}
} e[MAX * 10];
void add(int x, int y, int z){
e[cnt] = {y, z, head[x]};
head[x] = cnt++;
}
bool spfa(){
memset(t, 0, sizeof t);
queue <int> q;
memset(vis, 0, sizeof vis);
memset(dis, -INF, sizeof dis);
dis[0] = 0;
vis[0] = 1;
t[0]++;
q.push(0);
while(!q.empty()){
int u = q.front();
vis[u] = 0;
q.pop();
for(int i = head[u]; i != -1; i = e[i].next){
int to = e[i].to;
int c = e[i].c;
if(dis[to] < dis[u] + c){
dis[to] = dis[u] + c;
if(!vis[to]){
vis[to] = 1;
q.push(to);
t[to]++;
if(t[to] >= n)
return 0;
}
}
}
}
return 1;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
while(cin >> n >> m){
int flag = 0;
cnt = 0;
memset(head, -1, sizeof head);
for(int i = 0; i < m; i++){
char s[10];
int x, y, z;
cin >> s;
if(s[0] == 'P'){
cin >> x >> y >> z;
if(x == y && z != 0) flag = 1;
add(x, y, -z);
add(y, x, z);
}
else{
cin >> x >> y;
if(x == y) flag = 1;
add(y, x, 1);
}
}
for(int i = 0; i <= n; i++) add(0, i, 0);
if(flag) cout << "Unreliable" << endl;
else if(spfa()) cout << "Reliable" << endl;
else cout << "Unreliable" << endl;
}
}