总结一下:如果一个系统由n个变量和m个约束条件组成,其中每个约束条件形如xj-xi<=bk(i,j∈[1,n],k∈[1,m]),则称其为差分约束系统(system of difference constraints)。亦即,差分约束系统是求解关于一组变量的特殊不等式组的方法。
求解差分约束系统,可以转化成图论的单源最短路径(或最长路径)问题。
观察xj-xi<=bk,会发现它类似最短路中的三角不等式d[v]<=d[u]+w[u,v],即d[v]-d[u]<=w[u,v]。因此,以每个变量xi为结点,对于约束条件xj-xi<=bk,连接一条边(i,j),边权为bk。我们再增加一个源点s,s与所有定点相连,边权均为0。对这个图,以s为源点运行Bellman-ford算法(或SPFA算法),最终{d[ i]}即为一组可行解。地址连接:http://baike.baidu.com/link?url=LJEcrGOn0qnPkqRurNp_cNIVspO5GWygEIY3QYoHTfrvfzYrrupjCR6TiE7nf8W21PWvIUjcTnXwmOA5T8VE0_。
Time Limit: 3000MS | Memory Limit: 131072K | |
Total Submissions: 10264 | Accepted: 3197 |
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 Ndefense 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
代码如下:
<pre class="html" name="code">#include <algorithm> #include <iostream> #include <stdlib.h> #include <string.h> #include <stdio.h> #include <string> #include <queue> #include <cmath> #include <map> #define M 100000 #define INF 1 << 30; using namespace std; struct node { int u, v, w; }f[1500000]; int d[10100]; int n, m; int t, flat; void bellman_ford() { int i, j; for(i = 0; i < n; i++) { flat = 0; for(j = 0; j < t; j++) { if(d[f[j].u] > d[f[j].v]-f[j].w) { d[f[j].u] = d[f[j].v] - f[j].w; flat = 1; } } if(!flat) break; } if(flat) cout<<"Unreliable"<<endl; else cout<<"Reliable"<<endl; return; } int main() { int i; char s; int a, b, x; while(cin>>n>>m) { t = 0; memset(d , 0 , sizeof(d)); for(i = 0; i < m; i++) { getchar(); scanf("%c",&s); if(s == 'V') { scanf("%d %d",&a, &b); f[t].u = a; f[t].v = b; f[t++].w = 1; } else if(s == 'P') { scanf("%d %d %d",&a, &b, &x); f[t].u = a; f[t].v = b; f[t++].w = x; f[t].u = b; f[t].v = a; f[t++].w = -x; } } bellman_ford(); } return 0; }