码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 2983-Is the Information Reliable

时间:2018-06-13 23:28:29      阅读:192      评论:0      收藏:0      [点我收藏+]

标签:一个   memset   IV   term   range   tip   common   about   fine   

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

 

Solution:

  还是差分约束判断可行性。。。

  我们依然罗列出约束条件(可行性我直接当做求最小值来做):

    1、$s[v]-s[u]=c,s[u]-s[v]=c$,所以建边$w[u,v]=w[v,u]=c$(注意是双向的,因为等于是个对于两边都成立的约束条件

    2、$s[v]-s[u]>0$,即$s[v]-s[u]\geq 1$,所以建边$w[u,v]=1$

  记得新建一个源点,连向各点$w[1,i]=0,\;i\in 1\rightarrow n$,以保证图联通。

  然后跑一遍最长路,判断一下有无环输出就好了。

代码:

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<queue>
 5 #pragma GCC optimize(2)
 6 #define il inline
 7 #define For(i,a,b) for(int (i)=(a);(i)<=(b);(i)++)
 8 using namespace std;
 9 const int N=300005,M=1005,inf=23333333;
10 int h[M],to[N],net[N],w[N],cnt,dis[M],tot[M],n,m;
11 bool vis[M];
12 
13 il void add(int u,int v,int c){to[++cnt]=v,net[cnt]=h[u],h[u]=cnt,w[cnt]=c;}
14 
15 il void gi(int &a){
16     a=0;char x=getchar();bool f=0;
17     while((x<0||x>9)&&x!=-)x=getchar();
18     if(x==-)x=getchar(),f=1;
19     while(x>=0&&x<=9)a=(a<<3)+(a<<1)+x-48,x=getchar();
20 }
21 
22 il bool spfa(){
23     queue<int>q;
24     dis[0]=0;q.push(0);tot[0]++;
25     while(!q.empty()){
26         int u=q.front();q.pop();vis[u]=0;
27         for(int i=h[u];i;i=net[i])
28             if(dis[to[i]]<dis[u]+w[i]){
29                 dis[to[i]]=dis[u]+w[i];
30                 if(!vis[to[i]]){
31                     if(++tot[to[i]]>n)return 0;
32                     q.push(to[i]),vis[to[i]]=1;
33                 }
34             }
35     }
36     return 1;
37 }
38 
39 int main(){
40     while(~scanf("%d%d",&n,&m)){
41         char s[5];
42         cnt=0;
43         memset(dis,-0x3f,sizeof(int)*(n+3));
44         memset(vis,0,sizeof(int)*(n+3));
45         memset(tot,0,sizeof(int)*(n+3));
46         memset(h,0,sizeof(h));
47         int u,v,c;
48         while(m--){
49             scanf("%s",s);
50             gi(u),gi(v);
51             if(s[0]==P){
52                 gi(c);
53                 add(v,u,-c);add(u,v,c);
54             }
55             else add(u,v,1);
56         }
57         For(i,1,n) add(0,i,0);
58         if(spfa())puts("Reliable");
59         else puts("Unreliable");
60     }
61     return 0;
62 }

 

POJ 2983-Is the Information Reliable

标签:一个   memset   IV   term   range   tip   common   about   fine   

原文地址:https://www.cnblogs.com/five20/p/9177055.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!