POJ2983 Is the Information Reliable?
http://acm.sdut.edu.cn:8080/vjudge/contest/view.action?cid=267#problem/B
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
还是那句话:差分约束条件题目的难点是“怎么找到问题的约束条件”。
这题输入的边有两种格式:
1. 边长确定,即xi - xj = b; 可以转化成 xi - xj <= b 和 xi - xj >=b (即 xj - xi <= -b).
2. 边长不定,xi - xj >= 1; 可以转化成 xj - xi <= -1;
da-db>=x;
da-db<=x;
==>da>=db+x;
db>=da-x;
#include <iostream> #include <stdio.h> #include <string.h> #include <stdlib.h> using namespace std; int n,m,tt; struct node { int x,y,z; } q[2000001]; int dis[100001]; void BF() { int flag; memset(dis,0,sizeof(dis)); for(int i=1; i<=n; i++) { flag=0; for(int j=0; j<tt; j++) { if(dis[q[j].x]<dis[q[j].y]+q[j].z) { dis[q[j].x]=dis[q[j].y]+q[j].z; flag=1; } } if(flag==0) break; } if(flag) cout<<"Unreliable"<<endl; else cout<<"Reliable"<<endl; return; } int main() { char ch; int xx,yy,zz; while(scanf("%d%d",&n,&m)!=EOF) { tt=0; while(m--) { getchar(); scanf("%c",&ch); if(ch=='P') { scanf("%d%d%d",&xx,&yy,&zz); q[tt].x=xx; q[tt].y=yy; q[tt++].z=zz; q[tt].x=yy; q[tt].y=xx; q[tt++].z=-zz; } else if(ch=='V') { scanf("%d%d",&xx,&yy); q[tt].x=xx; q[tt].y=yy; q[tt++].z=1; } } BF(); } return 0; }