Layout poj3169 差分约束

来源:互联网 发布:阿里云企业邮箱 编辑:程序博客网 时间:2024/05/17 00:00

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.

(1)题意:n头牛编号为1到n,按照编号的顺序排成一列,每两头牛的之间的距离 >= 0。这些牛的距离存在着一些约束关系:1.有ml组(u, v, w)的约束关系,表示牛[u]和牛[v]之间的距离必须 <= w。2.有md组(u, v, w)的约束关系,表示牛[u]和牛[v]之间的距离必须 >= w。问如果这n头无法排成队伍,则输出-1,如果牛[1]和牛[n]的距离可以无限远,则输出-2,否则则输出牛[1]和牛[n]之间的最大距离。
(2)解法:记第i号牛的位置是d[i]。首先,牛是按照编号顺序排列的,所以有d[i]<=d[i+1]成立。其次,对于每对关系好的牛之间的最大距离限制,都有d[AL]+DL >=d[BL]成立。同样,对于每对关系不好的牛,都有d[AD]+DD<=d[BD]成立。因此,原问题可以转化为在满足这三类不等式的情况下,求解d的d[N] -d[1]的最大值问题。这是线性规划问题,可以使用单源最短路径算法求解。
这些不等式的特点是所有的式子的两边都只出现了1个变量。实际上,图上的最短路问题也可以用这样的形式表示出来。记从起点s出发,到各个顶点v的最短距离为d(v)。因此,对于每条权值为w的边e=(v,u),都有d(v)+w >=d(u)成立。反之,在满足全部这些约束不等式的d中,d(v)-d(s)的最大值就是从s到v的最短距离。需要注意这里不是最小值,而是最大值对应着最短距离。
把原来的问题和最短路问题进行比较就可以发现,两个问题都是完全一样的形式。也就是说,可以通过把原来的问题的每一个约束不等式对应成图中的一条边来构图,然后通过解决最短路问题来解决原问题。首先把顶点编号为1~N,d[i]<=d[i+1]变形为d[i+1]+0>=d[i],因此从顶点i+1向顶点i连一条权值为0的边。同样d[AL]+DL>=d[BL]对应从顶点BL连一条权值为DL的边,d[AD]+DD<=d[BD]对应从顶点BD向顶点AD连一条权值为-DD的边。所求的问题是d[N]-d[1]的最大值,对应为顶点1到顶点N的最短距离。由于图中存在负权边,因此不使用Dijkstra算法而是使用Bellman-Ford算法求解。即使这样复杂度也只有O(N(N+ML+MD))。

#include<cstdio>#include<cmath>#include<cstring>#include<algorithm>#include<iostream>using namespace std;#define INF 10000000#define MAX_ML 10005#define MAX_MD 10005#define MAX_N 1005//《挑战程序设计》111-112页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];//最短距离void solve(){    fill(d,d+N,INF);    d[0]=0;    //用bellman-ford计算d    for(int k=0;k<N;k++)    {        for(int i;i+1<N;i++)   //i+1到i的权值为0        {            if(d[i+1]<INF)                d[i]=min(d[i],d[i+1]);        }        for(int i=0;i<ML;i++)  //从AL到BL的权值为DL        {            if(d[AL[i]-1]<INF)                d[BL[i]-1]=min(d[BL[i]-1],d[AL[i]-1]+DL[i]);        }        for(int i=0;i<MD;i++)  //从BD到AD的权值为-DD        {            if(d[BD[i]-1]<INF)                d[AD[i]-1]=min(d[AD[i]-1],d[BD[i]-1]-DD[i]);        }    }    int res=d[N-1];    if(d[0]<0) res=-1;  //存在负圈,无解    else if(res==INF) res=-2;    printf("%d\n",res);}int main(){    while(scanf("%d %d %d",&N,&ML,&MD)!=EOF)    {        int i;        for(i=0;i<ML;i++)  scanf("%d %d %d",&AL[i],&BL[i],&DL[i]);        for(i=0;i<MD;i++)  scanf("%d %d %d",&AD[i],&BD[i],&DD[i]);        solve();    }    return 0;}
0 0
原创粉丝点击