Layout
Description
Like everyone else, cows like to stand close to their friends when queuing for feed. FJ has N (2 <= N <= 1,000) cows numbered 1…N standing along a straight line waiting for feed. The cows are standing in the same order as they are numbered, and since they can be rather pushy, it is possible that two or more cows can line up at exactly the same location (that is, if we think of each cow as being located at some coordinate on a number line, then it is possible for two or more cows to share the same coordinate).
Some cows like each other and want to be within a certain distance of each other in line. Some really dislike each other and want to be separated by at least a certain distance. A list of ML (1 <= ML <= 10,000) constraints describes which cows like each other and the maximum distance by which they may be separated; a subsequent list of MD constraints (1 <= MD <= 10,000) tells which cows dislike each other and the minimum distance by which they must be separated.
Your job is to compute, if possible, the maximum possible distance between cow 1 and cow N that satisfies the distance constraints.
Input
Line 1: Three space-separated integers: N, ML, and MD.
Lines 2…ML+1: Each line contains three space-separated positive integers: A, B, and D, with 1 <= A < B <= N. Cows A and B must be at most D (1 <= D <= 1,000,000) apart.
Lines ML+2…ML+MD+1: Each line contains three space-separated positive integers: A, B, and D, with 1 <= A < B <= N. Cows A and B must be at least D (1 <= D <= 1,000,000) apart.
Output
Line 1: A single integer. If no line-up is possible, output -1. If cows 1 and N can be arbitrarily far apart, output -2. Otherwise output the greatest possible distance between cows 1 and N.
Sample Input
4 2 1
1 3 10
2 4 20
2 3 3
Sample Output
27
Hint
Explanation of the sample:
There are 4 cows. Cows #1 and #3 must be no more than 10 units apart, cows #2 and #4 must be no more than 20 units apart, and cows #2 and #3 dislike each other and must be no fewer than 3 units apart.
The best layout, in terms of coordinates on a number line, is to put cow #1 at 0, cow #2 at 7, cow #3 at 10, and cow #4 at 27.
题意: 农夫约翰养了N头牛,编号从1到N。现在,他们要进食,按照编号顺序排成了一排。在它们之间有一些牛关系比较好,所以希望彼此之间不超过一定距离,也有一些牛关系比较不好,所以希望彼此之间至少要满足某个距离。此外,牛的性格比较犟,所以有可能有多头牛挤在同一个位置上。给出了ML个关系好的牛的信息(AL,BL,DL)以及MD个关系不好的牛的信息(AD,BL,DD)。这表示的是牛AL与牛BL之间的最大距离DL和牛AD与牛BD之间的最小距离DD。在满足这些条件的排列方法中,求1号牛和N号牛之间的最大距离。如果不存在任何一种排列方法满足条件则输出-1。无限大的情况输出-2。
题解: 记地i号牛的位置为d[i]。首先,牛是按照编号顺序排列的,所以有 d [ i ] ≤ d [ i + 1 ] d[i]\leq d[i+1] d[i]≤d[i+1]成立。其次,对于每对关系好的牛之间的最大距离的限制,都有 d [ A L ] + D L ≥ d [ B L ] d[AL] + DL \geq d[BL] d[AL]+DL≥d[BL]成立(因为AL和BL之间的最大距离是DL,所以AL与BL之间的距离是要比DL小的,通过移项即可得上式)。同样,对于每对关系不好的牛,都有 d [ A D ] + D D ≤ d [ B D ] d[AD] + DD \leq d[BD] d[AD]+DD≤d[BD]成立。因此,原问题可以转化为在满足这类不等式的情况下,求解d的d[N]-d[1]的最大值的问题。这是线性规划问题,可以使用单纯形法等较复杂的算法求解。但这道题有更简单的解法。
这些不等式的特点是所有的式子的两边都只出现了1个变量。实际上,图上的最短路问题也可以用这样的形式表示出来。记从起点s出发,到各个顶点v的最短路径距离为d(v)。因此,对于每条权值为w的边 e ( u , v ) e(u,v) e(u,v),都有 d ( u ) + w ≥ d ( v ) d(u)+w \geq d(v) d(u)+w≥d(v)成立。反之,在满足全部这些约束不等式的d中, d ( v ) − d ( s ) d(v)-d(s) d(v)−d(s)的最大值就是从s到v的最短距离。需要注意这里不是最小值,而是最大值对应着最短距离。
把原来的问题和最短路径问题进行比较就可以发现,两个问题是一样的形式。也就是说,可以通过把原来的问题的每一个约束不等式对应成图中的一条边来构图,然后通过解决最短路问题来解决原问题。首先把顶点编号为1~N。 d [ i ] ≤ d [ i + 1 ] d[i] \leq d[i+1] d[i]≤d[i+1]变形为 d [ i + i ] + 0 ≥ d [ i ] d[i+i] + 0 \geq d[i] d[i+i]+0≥d[i],因此从顶点i+1向顶点i连一条权值为0的边。同样 d [ A L ] + D L ≥ d [ B L ] d[AL] + DL \geq d[BL] d[AL]+DL≥d[BL]对应从顶点AL向顶点BL连一条权值为DL的边, d [ A D ] + D D ≤ d [ B D ] d[AD] + DD \leq d[BD] d[AD]+DD≤d[BD]对应从顶点BD向顶点AD连一条权值为 -DD的边。所求问题是 d [ N ] − d [ 1 ] d[N]-d[1] d[N]−d[1]的最大值,对应为顶点1到顶点N的最短距离。由于图中存在负圈边,因此不使用Dijkstra算法而是使用Bellmam-Ford算法求解。即使这样复杂度也只有 O ( N ( N + M L + M D ) ) O(N(N+ML+MD)) O(N(N+ML+MD)),可以在规定时间内求解。
c++ AC 代码
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define INF 0x3f3f3f3f
using namespace std;
const int MAX_MD = 1e4 + 10, MAX_ML = 1e4 + 10, MAX_N = 1e4 + 10;
int N, ML, MD;
int AL[MAX_ML],BL[MAX_ML],DL[MAX_ML];
int AD[MAX_MD],BD[MAX_MD],DD[MAX_MD];
int d[MAX_N]; // 最短距离
bool updated; // 是否有更新
void update(int &x, int y)
{
if(x > y)
{
x = y;
updated = true;
}
}
void bellmanford()
{
for(int k=0;k<=N;k++)
{
updated = false;
// 从i+1到i的权值为0
for(int i=0;i+1<N;i++)
if(d[i+1] < INF) update(d[i],d[i+1]);
// 从AL到BL的权值为DL
for(int i=0;i<ML;i++)
if(d[AL[i]-1] < INF)
update(d[BL[i] - 1],d[AL[i] - 1] + DL[i]);
// 从BD到AD的权值为 -DD
for(int i=0;i<MD;i++)
if(d[BD[i] - 1] < INF)
update(d[AD[i] - 1], d[BD[i] - 1] - DD[i]);
}
}
void solve()
{
// 检查是否存在负圈
fill(d,d+N,0);
bellmanford();
if(updated) // 第N次更新了,说明存在负圈
{
puts("-1");
return ;
}
// 求解
fill(d,d+N,INF);
d[0] = 0;
bellmanford();
int res = d[N - 1];
if(res == INF)
res = -2;
printf("%d\n",res);
}
int main()
{
scanf("%d%d%d",&N,&ML,&MD);
for(int i=0;i<ML;i++)
scanf("%d%d%d",AL+i,BL+i,DL+i);
for(int i=0;i<MD;i++)
scanf("%d%d%d",AD+i,BD+i,DD+i);
solve();
return 0;
}
文章来自《挑战程序设计竞赛第2版》2.5节