Is the information Reliable?
Time Limit: 3000MS |
|
Memory Limit: 131072K |
Total Submissions: 11859 |
|
Accepted: 3742 |
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 and 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 ' AR Rangement 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 P A B X
is in the form of, means defense station A was X light-years north of Defense station C4>b.
Vague Tip V A B
is in the form of, means defense station A was in the north of Defense station B, at LEAs T 1 light-year, but the precise distance is unknown.
Input
There is several test cases in the input. Each test case is starts with the integers n (0 < n ≤1000) and m (1≤ m ≤100000). The next M line is 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 o Utput "unreliable".
Sample Input
3 4P 1 2 1P 2 3 1V 1 3P 1 3 5V 1 2V 2 3V 3 4V 4 5V 3 5
Sample Output
Unreliablereliable
Source
POJ monthly--2006.08.27, Dagger
http://blog.csdn.net/lyy289065406/article/details/6648688
AC Code
#include <stdio.h> #include <string.h> #include <queue> #include <string> #include <iostream > #define INF 0xfffffffusing namespace Std;int head[1010],vis[1010],cnt,dis[1010],s,cot[1010];int n,m;struct s{int u , V,w,next;} edge[300200];void Add (int u,int v,int W) {edge[cnt].u=u;edge[cnt].v=v;edge[cnt].w=w;edge[cnt].next=head[u];head[u]= cnt++;} int SPFA (int s) {int i;for (i=1;i<=n;i++) {dis[i]=inf;} memset (vis,0,sizeof (Vis)), memset (cot,0,sizeof (cot));d Is[s]=0;vis[s]=1;queue<int>q;q.push (s); Q.empty ()) {int U=q.front (); Q.pop (); vis[u]=0;cot[u]++;if (cot[u]>n) return 0;for (I=head[u];i!=-1;i=edge[i].next) {int V=edge[i].v;int w=edge[i].w;if (dis[v]>dis[u]+w) {dis[v]=dis[u]+w;if (!vis[v]) {Vis[v]=1;q.push (v);}}}} return 1;} int main () {//int n,m;while (scanf ("%d%d", &n,&m)!=eof) {char str[2];int i;memset (head,-1,sizeof (head)); cnt=0; for (i=0;i<m;i++) {scanf ("%s", str), if (str[0]== ' P ') {int a,b,c;scanf ("%d%d%d", &a,&b,&c); add (a,b,c); Add (b,a,-c);} Else{int A,b;sCANF ("%d%d", &a,&b); add (b,a,-1);}} S=n+1;for (i=1;i<=n;i++) {Add (s,i,0);} if (SPFA (s)) printf ("reliable\n"); elseprintf ("unreliable\n");}}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
POJ Topic 2983 is the information Reliable? (differential constraint)