hdu Trilogy 1 Is the Information Reliable? differential constraints bellman_ford algorithm

Problem 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
***************************************************************************************************************************
Differential constraints map building is very important.
***************************************************************************************************************************
 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 }
View Code

Focusing to find the relationship (constraints), built in Fig.

Reproduced in: https: //www.cnblogs.com/sdau--codeants/p/3352266.html

Guess you like

Origin blog.csdn.net/weixin_34112030/article/details/93432936