题目:Layout
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.
InputSome 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.
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.
OutputLines 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.
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 Input4 2 1 1 3 10 2 4 20 2 3 3Sample Output
27Hint
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.
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.
这里我们用《挑战》上的题解,运用Bellman-Ford算法。题目中两点间的路径长度是可以变化的,但结尾让求最大值,那么可以在变化区间内将最符合的数字设为路径值。
设d[i]为到点1的距离。
第一点根据题意,d[i]<=d[i+1],变化一下是d[i]-d[i+1]<=0,草图上画出i+1指向i的有向路径0;
第二点,若互相喜欢,d[a]+L>=d[b],即d[b]-d[a]<=L,同理,a指向b的路径设为最大的L;
第三点,若相互讨厌,d[a]+L<=d[b],即d[a]-d[b]<=-L,b指向a的路径设为-L。
我们看上面三种情况,虽然都是d[x]-d[y]<=L的形式,代表y指向x的路径,但第二点当中x是大于y的,L也是大于0的,我们设路径为L的原因是让更大的点b尽量靠右,尽可能使最终结果更大;而第一点和第三点中x<y,L<=0,我们设其为路径的原因是让较小点a尽量靠右,尽可能使结果更大。自行脑补一个数轴即可理解。
把路径固定以后求最短路即可,由于有负边不能用Dijkstra算法。这里要求最短路而不是最长路的原因是:我们上面已经把路径设为最大了,已经不能再大了,再大就违反规则了,也就是我们已经把一个求最大距离的题目转化为求最短距离的题目了。试想,比如点1和点3互相喜欢,1到3有一条10的路,有一条20的路,不同的路就是对这两点的不同规则,我们要取规则们的交集而不是并集,即两点之间要同时符合所有要求,如果选了20,那么就违反了1到3不能超过10这一条。
上述皆为自己胡乱理解,有哪里错误还请指正。书中的解释在111页,大家也可以搜索更好的算法。代码如下:
#include<iostream>
#include<cstdio>
#define MAX 10001
#define inf 0x7ffffff
using namespace std;
int n,ml,md;
int al[MAX],bl[MAX],dl[MAX],ad[MAX],bd[MAX],dd[MAX],d[MAX];
//命名中以带a的为小点,带b的为大点,带l的为喜欢,带d的为讨厌
int main()
{
cin>>n>>ml>>md;//个数、喜欢的边、讨厌的边
for(int i=1;i<=ml;i++)//喜欢的点和路径
scanf("%d%d%d",&al[i],&bl[i],&dl[i]);
for(int i=1;i<=md;i++)//讨厌的点和路径
scanf("%d%d%d",&ad[i],&bd[i],&dd[i]);
fill(d+2,d+1+n,inf);//d[1]=0就在全局里默认了
for(int k=1;k<n;k++)
{
for(int i=1;i<n;i++)//i+1到i的路径为0
if(d[i+1]!=inf)
d[i]=min(d[i+1],d[i]);
for(int i=1;i<=ml;i++)//喜欢的路径进行松弛
if(d[al[i]]!=inf)
d[bl[i]]=min(d[bl[i]],d[al[i]]+dl[i]);
for(int i=1;i<=md;i++)//讨厌的路径进行松弛
if(d[bd[i]]!=inf)
d[ad[i]]=min(d[ad[i]],d[bd[i]]-dd[i]);
}
int res=d[n];
if(d[1]<0)//如果有负圈,在“i+1到i为0”这一步会波及d[1],负圈则规则间相互违反
res=-1;
else if(res==inf)//没有1到n的路径则随意了
res=-2;
cout<<res;
return 0;
}