Is the Information Reliable?
Time Limit: 3000MS Memory Limit: 131072K
Total Submissions: 13588 Accepted: 4262
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
题意:一条南北线上有n个防御站,给出他们之间的位置关系,问有没有可能存在这样一种位置布置符合所给的位置关系。关系有两种,一种是 P A B X,表示A在B北边X光年的位置,V A B表示A在B北边至少1光年位置。
解法:差分约束。
dist[A]-dist[B]>=X,表示A在B北边至少X光年位置。变形为:dist[B]<=dist[A]-X;所以A指向B有条权值为-X的边。这也是最短路松弛原理的关系。对于A-B=X,则建两条边dist[B]<=dist[A]-X,dist[A]<=dist[B]+X。
SPFA时候如果存在负权环则说明不符合实际情况。
代码如下:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>
using namespace std;
#define eps 1e-8
#define zero(_) (abs(_)<=eps)
const double pi=acos(-1.0);
typedef long long LL;
const int Max=1010;
const LL INF=0x3FFFFFFF;
struct point
{
int u;
int dist;
};
vector<point> vec[Max];
int dis[Max];
int que[Max*Max];
int rem[Max];
int n,m;
bool spfa1()
{
memset(dis,0x3f,sizeof(dis));
memset(rem,0,sizeof(rem));
que[0]=0;
int left=0,right=1;
dis[0]=0;
while(left<right)
{
int t=que[left++];
for(int i=0; i<vec[t].size(); i++)
{
if(dis[vec[t][i].u]>dis[t]+vec[t][i].dist)
{
dis[vec[t][i].u]=dis[t]+vec[t][i].dist;
que[right++]=vec[t][i].u;
rem[vec[t][i].u]++;
if(rem[vec[t][i].u]>n+2) return false;
}
}
}
return true;
}
int main()
{
while(scanf("%d%d",&n,&m)==2)
{
vec[0].clear();
for(int i=1; i<=n; i++)
{
vec[i].clear();
point p;
p.u=i;
p.dist=1;
vec[0].push_back(p);
}
bool flag=1;
for(int i=0; i<m; i++)
{
char c;
getchar();
scanf("%c",&c);
// getchar();
if(c=='P')
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
point p;
p.dist=-c;
p.u=b;
vec[a].push_back(p);
p.dist=c;
p.u=a;
vec[b].push_back(p);
}
else
{
int a,b;
scanf("%d%d",&a,&b);
point p;
p.dist=-1;
p.u=b;
vec[a].push_back(p);
}
}
if(spfa1()) puts("Reliable");
else puts("Unreliable");
}
return 0;
}