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.
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 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”.
1 #include<iostream> 2 #include<string> 3 #include<cstring> 4 #include<cstdio> 5 #include<cctype> 6 #include<queue> 7 #include<stack> 8 using namespace std; 9 struct edge 10 { 11 int v,u,len; 12 }e[200011]; 13 bool change; 14 int dis[1021]; 15 int e_num,n,m; 16 char c; 17 int x,y,w; 18 void add(int x,int y,int len) 19 { 20 e[e_num].v=x; 21 e[e_num].u=y; 22 e[e_num].len=len; 23 e_num++; 24 } 25 bool bellman_ford() 26 { 27 for(int i=0;i<n;i++) 28 { 29 change=false; 30 for(int j=0;j<e_num;j++) 31 { 32 if(dis[e[j].u]>dis[e[j].v]+e[j].len) 33 { 34 dis[e[j].u]=dis[e[j].v]+e[j].len; 35 change=true; 36 } 37 } 38 } 39 return !change; 40 } 41 int main() 42 { 43 while(scanf("%d %d",&n,&m)==2) 44 { 45 int i,j,k; 46 e_num=0; 47 memset(dis,0,sizeof(dis)); 48 for(i=0;i<m;i++) 49 { 50 scanf("\n%c %d %d",&c,&x,&y); 51 //满足x-y>=w&&y-x<=-w 52 if(c=='P') 53 { 54 scanf(" % D " , & W); 55 the Add (Y, X, W); 56 is the Add (X, Y, - W); 57 is } 58 the else 59 { 60 // satisfies XY <= -. 1 61 is the Add (X, Y, - . 1 ); 62 is } 63 is } 64 IF (bellman_ford ()) // if there is a negative ring, contradictory, untrusted 65 the printf ( " Reliable \ n- " ); 66 the else 67 the printf ( " Unreliable \ n- "); 68 } 69 return 0; 70 }
Focusing to find the relationship (constraints), built in Fig.
Reproduced in: https: //www.cnblogs.com/sdau--codeants/p/3352266.html