poj 2983 差分约束+SPFA
来源:互联网 发布:sql默认地址不详设置 编辑:程序博客网 时间:2024/03/29 01:52
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 4P 1 2 1P 2 3 1V 1 3P 1 3 15 5V 1 2V 2 3V 3 4V 4 5V 3 5
Sample Output
UnreliableReliable
Source
#include<iostream>#include<cstdio>#include<queue>#include<cstring>using namespace std;struct node{ int a,b,c; int next;}e[202005];int fst[1005];bool v[1005];int m,n,a,b,c,num;int cnt[1005];char p[5];int dist[1005];bool spfa()//spfa判断是否有解{ for(int i=0;i<=n;i++)dist[i]=1;//比0点到它的边的长度大即可 dist[0]=0; queue<int>q; q.push(0); cnt[0]++; v[0]=1; while(!q.empty()) { a=q.front(); q.pop(); for(int i=fst[a];i!=-1;i=e[i].next) { b=e[i].b; c=e[i].c; if(dist[b]>dist[a]+c) { dist[b]=dist[a]+c; if(!v[b]) { q.push(b); v[b]=1; cnt[b]++; if(cnt[b]>n)return false;//说明有负环路,无解退出 } } } v[a]=0; } return true;}int main(){ int ans; while(scanf("%d%d",&n,&m)!=EOF) { num=0; ans=0; memset(fst,-1,sizeof(fst)); memset(cnt,0,sizeof(cnt)); memset(v,0,sizeof(v)); for(int i=0;i<m;i++) { scanf("%s",&p); if(p[0]=='P') { scanf("%d%d%d",&a,&b,&c); if(a==b&&c)ans=1;//自己连自己的点特殊处理 e[num].next=fst[a];//等式变为俩不等式,即建立双向边,这里邻接表存储 fst[a]=num; e[num].a=a; e[num].b=b; e[num++].c=-c; e[num].next=fst[b]; fst[b]=num; e[num].a=b; e[num].b=a; e[num++].c=c; } else { scanf("%d%d",&a,&b); if(a==b)ans=1;//自己连自己的特殊处理 e[num].next=fst[a];//不等式只建立单向边 fst[a]=num; e[num].a=a; e[num].b=b; e[num++].c=-1; } } for(int i=1;i<=n;i++) { e[num].next=fst[0]; fst[0]=num; e[num].a=0; e[num].b=i; e[num++].c=0; } if(ans)cout<<"Unreliable"<<endl; else if(spfa())cout<<"Reliable"<<endl; else cout<<"Unreliable"<<endl; } return 0;}
- POJ 2983 SPFA 差分约束
- poj 2983 差分约束+SPFA
- poj 2983 spfa差分约束
- POJ-2983 用SPFA求解差分约束..
- POJ 2983 (差分约束系统Spfa)
- poj 2983 差分约束 spfa和bellman_fond算法
- POJ 1201 Intervals 差分约束+spfa
- POJ 3159 Candies 差分约束+spfa
- poj 3159 Candies 差分约束 + spfa
- poj 1201 Intervals 差分约束 + spfa
- poj 3169 差分约束 + spfa
- POJ 1364 King 差分约束 SPFA
- POJ-1716 同上..SPFA差分约束..
- POJ 1201 SPFA 差分约束
- POJ 1716 差分约束 SPFA
- poj 1201-差分约束+spfa
- poj 1364 SPFA+差分约束
- poj 1201 Intervals 差分约束+spfa
- Bookmarks--How to establish Android Develop Environment
- android获取正在运行的桌面的包名
- 关于客户问题反馈的类型机制
- Windows 进程间通信(八)共享内存(FileMapping)
- MZ自测——遥远的村庄(并查集)
- poj 2983 差分约束+SPFA
- 一点基础的东西:Objective-C的类型和常量
- struts2中怎么获取session,jsp页面参数信息的值
- 在Eclipse中使用JUnit4进行单元测试
- 贪心 hdu-1338-Game Prediction
- XMPP——Smack[6]离线消息和离线文件的实现
- 金融支付-银联卡支付系统产品介绍
- CScrollView滚动条范围问题 超过32位不能正确下拉问题
- python 引用和拷贝