题目
Is the Information Reliable?
Time Limit: 3000MS Memory Limit: 131072K
Total Submissions: 12490 Accepted: 3930
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 A B X ,表示a在b的前面x米。
V A B ,A在B前面不知道多少米。问这些信息有没有矛盾
题解
有
d[a]-d[b]=X
这个结果可以用两个不等式表示
d[a]-d[b]>=x&&d[a]-d[b]<=x
a至少在b前1米
即d[a]>=d[b]+1
按这些约束条件进行Bllman-ford
*用c++提交AC,用G++提交RE
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <stack>
#include <string>
#include <set>
#include <cmath>
#include <map>
#include <queue>
#include <sstream>
#include <vector>
#include <iomanip>
#define m0(a) memset(a,0,sizeof(a))
#define mm(a) memset(a,0x3f,sizeof(a))
#define m_1(a) memset(a,-1,sizeof(a))
#define f(i,a,b) for(i = a;i<=b;i++)
#define fi(i,a,b) for(i = a;i>=b;i--)
#define lowbit(a) ((a)&(-a))
#define FFR freopen("data.in","r",stdin)
#define FFW freopen("data.out","w",stdout)
#define INF 0x3f3f3f3f
typedef long long ll;
typedef long double ld;
const ld PI = acos(-1.0);
using namespace std;
#define SIZE ( )
struct Edge {
int u, v, k;
};
struct Edge1 {
Edge edge[100000 + 10];
int cnt;
void push_back(int u, int v, int k) {
edge[cnt].u = u;
edge[cnt].v = v;
edge[cnt].k = k;
cnt++;
}
};
Edge1 G;
int d[1234];
int main() {
//ios_base::sync_with_stdio(false); cin.tie(0);
int n, m;
while (~scanf("%d%d",&n,&m)) {
char s[5];
int i,j;
G.cnt = 0;
f(i, 1, m) {
scanf("%s", s);
if ('P' == s[0]) {
int u, v, k;
scanf("%d%d%d", &u, &v, &k);
G.push_back(u, v, -k);
G.push_back(v, u, k);
} else {
int u, v;
scanf("%d%d", &u, &v);
G.push_back(u, v, -1);
}
}
m0(d);
int ok = 0;
f(i, 1, n) {
ok = 0;
f(j, 0, G.cnt - 1) {
if(d[G.edge[j].v] > d[G.edge[j].u] + G.edge[j].k){
d[G.edge[j].v] = d[G.edge[j].u] + G.edge[j].k;
ok = 1;
}
}
if(!ok){
ok = 1;
break;
}
}
if (i<=n) printf("Reliable\n");
else printf("Unreliable\n");
}
return 0;
}