"POJ 2983" is the information Reliable? (differential constraint system)
Is the information Reliable?
Time Limit: 3000MS |
|
Memory Limit: 131072K |
Total Submissions: 12244 |
|
Accepted: 3861 |
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 withN defense stations. Because of the cooperation of the stations, Zibu ' s Marine Glory cannot march any further and 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 ' AR Rangement 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 P A B X
is in the form of, means defense station A was X light-years north of Defense station C4>b.
Vague Tip V A B
is in the form of, means defense station A was in the north of Defense stationB, at least 1 light-year, but the precise distance is unknown.
Input
There is several test cases in the input. Each test case is starts with the integersn (0 < n ≤1000) and m (1≤ m ≤100000). The nextM line is 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 arrangeN Defense stations satisfying all the M tips, otherwise OUs Tput "unreliable".
Sample Input
3 4P 1 2 1P 2 3 1V 1 3P 1 3 5V 1 2V 2 3V 3 4V 4 5V 3 5
Sample Output
Unreliablereliable
Source
POJ monthly--2006.08.27, Dagger
Establishing a Differential constraint system for solving
There are two kinds of relationships
P a B x means a on the north Side X light years equivalent to PB-PA = X
Want to represent equals to convert to Pb-pa >= x pb-pa <= x
That is Pb-pa >= x PA-PB >=-X
V A B means that a is at least a light-year north of B pb-pa >= 1
Using three formulas to establish a differential constraint system it is necessary to link them with a super source because it is possible to have multiple non-connected graphs.
If the shortest running process does not have a negative ring that is a valid diagram otherwise unreliable
The code is as follows:
#include <iostream> #include <cmath> #include <vector> #include <cstdlib> #include <cstdio > #include <cstring> #include <queue> #include <list> #include <algorithm> #include <map > #include <set> #define LL long Long#define fread () freopen ("In.in", "R", stdin) #define FWRITE () freopen (" Out.out "," w ", stdout) using namespace std;const int INF = 0x3f3f3f3f;const int msz = 10000;const double eps = 1e-8;int dcmp ( Double x) {return x <-eps -1:x > eps;} struct edge{int v,w,next;}; Edge eg[233333];int head[1010];int dis[1010];int cnt[1010];bool vis[1010];int n,m,tp;void Add (int u,int v,int W) {EG[TP]. v = V;EG[TP].W = W;eg[tp].next = Head[u];head[u] = tp++;} BOOL Spfa () {memset (dis,-inf,sizeof (DIS)), memset (cnt,0,sizeof (CNT)), memset (vis,0,sizeof (Vis)), vis[0] = 1;dis[0] = 0; Cnt[0]++;queue <int> q;q.push (0); int u,v,w;while (!q.empty ()) {u = Q.front (); Vis[u] = 0;q.pop (); for (int i = head[u]; I! =-1; i = eg[i].next) {v = eg[i].v;w = Eg[i].w;if (diS[V] < Dis[u]+w) {Dis[v] = dis[u]+w;cnt[v]++;if (Cnt[v] > N) return false;if (!vis[v]) {Q.push (v); vis[v] = 1;}}} return true;} int main () {int U,v,w;char opt[3];while (~scanf ("%d%d", &n,&m)) {memset (head,-1,sizeof (head)); tp = 0;while (m--) {scanf ("%s", opt), if (opt[0] = = ' P ') {scanf ("%d%d%d", &u,&v,&w); ADD (U,V,W); ADD (v,u,-w);} else {scanf ("%d%d", &u,&v); ADD (u,v,1);}} for (int i = 1; I <= n; ++i) {Add (0,i,0);} Puts (SPFA ()? "Reliable": "Unreliable");} return 0;}
"POJ 2983" is the information Reliable? (differential constraint system)