Time Limit: 3000MS | Memory Limit: 131072K | |
Total Submissions: 13088 | Accepted: 4126 |
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个点m个限制条件,关于每个条件:p a b c 表示a站比b站向北多出c个光年,v a b表示a比b至少多出1个光年,但是具体是多少并不知道。
思路:首先我们要明确,它怎么样就是信息错误了,如果我们把它抽象成一个平面图,那么就是:
拿第一个示例来说,我们把确切的边建两条边权值分别为正负。之后的v边是一个不等式a-b>=1那么就是b-a<-1,利用差分约束的条件建出一条边a到b权值为-1;
那么不满足条件的自然就是一个负环了
1.v,p同时存在构成的环
像那种-1(v) -1(v) 4(p)那种构成环肯定符合条件。
而把4(p)换成1(p)自然是不可能符合条件的。这种情况是负环
2.自相矛盾的就是个负环(a比b高2,b比c高3,而a却比c高10)。
也就是说,符合条件的你这个环走完一圈应该是权值加和为0的,因为有v的存在所以可以存在类似正环的东西。
注意:
用spfa来解决差分约束系统的时候注意,增加一个源点!!不然你觉得你应该从哪个点开始?其实是应该全部点都搜一次的不过那样也太麻烦了。
像这个题肯定是判断所有的点的,所以要确保开始所有点都能进入队列来检查是否能更新即可!
#include <iostream>
#include <cstdio>
#include <queue>
#include <algorithm>
using namespace std;
const int MAXN=1000+10;
const int inf=1e9;
int n,m;
int cnt;
bool vis[MAXN];
int dis[MAXN];
int sum[MAXN];
int head[MAXN];
struct node
{
int v;
int w;
int next;
}e[210000];
void add(int u,int v,int w)
{
e[cnt].v=v;
e[cnt].w=w;
e[cnt].next=head[u];
head[u]=cnt++;
}
bool spfa()//这东西就是用来检查负环的
{
int i;
for(i=0;i<=n;++i)
{
dis[i]=inf;
sum[i]=0;
vis[i]=0;
}
queue<int>q;
q.push(0);
int u;
while(!q.empty())
{
u=q.front();
q.pop();
vis[u]=0;
for(i=head[u];i!=-1;i=e[i].next)
{
int v=e[i].v;
int w=e[i].w;
int t=dis[u]+w;
if(dis[v]>t)
{
dis[v]=t;
if(!vis[v])
{
sum[v]++;
if(sum[v]>n)return 0;
q.push(v);
vis[v]=1;
}
}
}
}
return 1;
}
void init()
{
cnt=0;
for(int i=0;i<=n;++i)head[i]=-1;
}
int main()
{
char s[2];
int i;
while(~scanf("%d%d",&n,&m))
{
init();
for(i=1;i<=n;++i)
{
add(0,i,-1);
//增加一个负边权,确保每个点都能进入队列来检查是否能更新
}
int u,v,w;
for(i=0;i<m;++i)
{
scanf("%s",s);
if(s[0]=='P')
{
scanf("%d%d%d",&u,&v,&w);//这个是确定的边
add(u,v,-w);
add(v,u,w);
}
else
{
scanf("%d%d",&u,&v);//根据差分约束系统构造的边
add(u,v,-1);
}
}
if(spfa())puts("Reliable");
else puts("Unreliable");
}
return 0;
}