Time Limit: 3000MS | Memory Limit: 131072K | |
Total Submissions: 9894 | Accepted: 3069 |
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
题意:给出m个信息,每个信息有的是P,A,B,X,表示A在B的北面,距离X光年;有的是V,A,B,表示A在B的北面,距离至少为1光年。问这些信息是否可靠。
思路:差分约束,看是否有解,用spfa判负环即可。对于每个关系A-B=X,可化为x=<A-B<=x,建两条边:B->A,权值为X和A->B,权值为-X。对于每个关系A-B>=1,建一条边A->B,权值为-1.另外,此题存在图不连通的情况,所以要设一个超级源点,连到其他所有点,权值为0.
AC代码:
#include <iostream> #include <cstdio> #include <cstring> #include <string> #include <algorithm> #include <queue> #include <vector> #include <cmath> #include <stack> #include <map> using namespace std; const int INF=100000000; const int maxn=1005; struct node { int v,w,next; }edge[202000]; int head[maxn],dis[maxn],cnt[maxn]; bool vis[maxn]; int num,n,m; void init() { for(int i=0;i<=n;i++) head[i]=-1; num=0; } void add(int u,int v,int w) { edge[num].v=v; edge[num].w=w; edge[num].next=head[u]; head[u]=num++; } void spfa() { queue<int>Q; for(int i=0;i<=n;i++) dis[i]=INF; dis[0]=0; memset(vis,false,sizeof(vis)); memset(cnt,0,sizeof(cnt)); vis[0]=true; Q.push(0); cnt[0]++; while(!Q.empty()) { int x=Q.front(); Q.pop(); vis[x]=false; for(int i=head[x];i!=-1;i=edge[i].next) { if(dis[edge[i].v]>dis[x]+edge[i].w) { dis[edge[i].v]=dis[x]+edge[i].w; if(!vis[edge[i].v]) { vis[edge[i].v]=true; if(++cnt[edge[i].v]>n) { printf("Unreliable\n"); return; } Q.push(edge[i].v); } } } } printf("Reliable\n"); } int main() { int a,b,c; char kind[5]; while(scanf("%d%d",&n,&m)!=EOF) { init(); while(m--) { scanf("%s",kind); if(kind[0]=='P') { scanf("%d%d%d",&a,&b,&c); add(b,a,c); add(a,b,-c); } else { scanf("%d%d",&a,&b); add(a,b,-1); } } for(int i=1;i<=n;i++) add(0,i,0); spfa(); } return 0; }