Time Limit: 3000MS | Memory Limit: 131072K |
---|
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.node
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.ios
The information consists of M tips. Each tip is either precise or vague.markdown
Precise tip is in the form of P A B X, means defense station A is X light-years north of defense station B.ide
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.oop
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.ui
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」.spa
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 5code
Unreliable
Reliableorm
POJ Monthly–2006.08.27, Daggerip
題意:有n個防護塔和m個情報,判斷這些情報是否是可信,情報有兩種:(1):P u v w 表示u在v的w光年處 。(2):V u v 表示u在v北的一光年以外(包括一光年)
思路:Dis[i]表示i在源點北面Dis[i]光年,因此對於(1):P uv w 能夠表示爲Dis[u]-Dis[v]=w,因此Dis[u]-Dis[v]>=w且Dis[v]-Dis[u]>=-w,對於(2): V u v 能夠表示爲Dis[u]-Dis[v]>=1,因此創建圖,跑最長路,判斷是否是有矛盾即正環,我用的
SPFA,要判斷自環。
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <queue>
#include <string>
#include <vector>
#include <stack>
#include <iostream>
#include <algorithm>
using namespace std;
const int INF = 0x3f3f3f3f;
const int MaxN = 1100;
const int MaxM = 100100;
typedef struct node
{
int v,w,next;
}Line;
Line Li[MaxM*2];
int Head[MaxN],top;
int Dis[MaxN],Du[MaxN];
bool vis[MaxN];
int n,m;
void AddEdge(int u,int v,int w)
{
Li[top].v = v ; Li[top].w = w;
Li[top].next = Head[u];
Head[u] = top++;
}
bool SPFA()
{
queue<int>Q;
for(int i=1;i<=n;i++)//至關於有一個超級源點
{
Dis[i] = 0;
vis[i]=true;
Du[i]=1;
Q.push(i);
}
while(!Q.empty())
{
int u = Q.front();
Q.pop();
if(Du[u]>n)//有矛盾
{
return false;
}
for(int i = Head[u];i!=-1;i = Li[i].next)
{
int v = Li[i].v;
if(Dis[v]<Dis[u]+Li[i].w)
{
Dis[v] = Dis[u]+Li[i].w;
if(!vis[v])
{
Q.push(v);
vis[v] = true;
Du[v]++;
}
}
}
vis[u]=false;
}
return true;
}
int main()
{
char Op[5];
int u,v,w;
while(~scanf("%d %d",&n,&m))
{
memset(Head,-1,sizeof(Head));
top = 0;
bool flag=false;
for(int i=1;i<=m;i++)
{
scanf("%s",Op);
if(Op[0]=='P')
{
scanf("%d %d %d",&u,&v,&w);
if(u==v&&w!=0)//自環
{
flag=true;
}
AddEdge(u,v,w);
AddEdge(v,u,-w);
}
else
{
scanf("%d %d",&u,&v);
if(u==v)
{
flag=true;
}
AddEdge(u,v,1);
}
}
if(flag||!SPFA())
{
printf("Unreliable\n");
}
else
{
printf("Reliable\n");
}
}
return 0;
}