POJ3169差分约束

来源:互联网 发布:广电网络客服 编辑:程序博客网 时间:2024/05/15 04:41

一叶落寞,万物失色。

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 11 3 102 4 202 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,按照编号的顺序排成一列,每两头牛的之间的距离 >= 0。这些牛的距离存在着一些约束关系:1.有ml组(u, v, w)的约束关系,表示牛[u]和牛[v]之间的距离必须 <= w。2.有md组(u, v, w)的约束关系,表示牛[u]和牛[v]之间的距离必须 >= w。问如果这n头无法排成队伍,则输出-1,如果牛[1]和牛[n]的距离可以无限远,则输出-2,否则则输出牛[1]和牛[n]之间的最大距离。
题解:差分约束,比较容易看出来的,不会差分去http://www.cppblog.com/menjitianya/archive/2015/11/19/212292.html  看完博客应该就比较清晰的理解差分约束啦,对于这个题,首先我们建立一个有向图,我们约定边又小标号的点指向大标号的点,如果是连个的距离不大于一个数K,由差分可以知道。A[I]-B[I]<=C[I] 符合差分系统的建图的,我们直接建边即可,如果是不小于的话那么由A[I]-B[I]>=C[I]是不符合差分系统建图的,于是我们变下号变为B[I]-A[I]<=-C[I]再建图即可解决
代码:
#include <algorithm>#include <cstdio>#include <cstring>#define inf 1<<25using namespace std;int read(){char ch;int s=0,f=1;ch=getchar();while(ch>'9'||ch<'0') { if(ch=='-') f*=-1;ch=getchar(); }while(ch>='0'&&ch<='9') s=s*10+ch-48,ch=getchar();return s*f;}struct node { int y,nxt,v,x;}e[20010];int head[1005],cnt;void add(int x,int y,int v){    cnt++;    e[cnt].x=x,e[cnt].y=y,e[cnt].nxt=head[x],e[cnt].v=v;    head[x]=cnt;}int n,m,ml,md;int dis[1005];int spfa(){    for(int i=1;i<=n;i++) dis[i]=inf;    dis[1]=0;    for(int i=1;i<=n;i++)    {        for(int j=1;j<=m;j++)        {            int u,v,w;            u=e[j].x,v=e[j].y,w=e[j].v;            if(dis[u]<inf&&dis[u]+w<dis[v])                dis[v]=dis[u]+w;        }    }    for(int i=1;i<=n;i++)    {        for(int j=1;j<=m;j++)        {            int u,v,w;            u=e[j].x,v=e[j].y,w=e[j].v;            if(dis[u]<inf&&dis[u]+w<dis[v])               return 0;        }    }    return 1;}void sort(int *a, int *b){    int t;    if(*a > *b){        t = *a; *a = *b; *b = t;    }}int main(){ scanf("%d %d %d", &n, &ml, &md);    m=ml+md;    int u,v,w;   for(int i=0; i<ml; i++){        scanf("%d %d %d", &u, &v, &w);        sort(&u, &v);        add(u, v, w);    }    for(int i=0; i<md; i++){        scanf("%d %d %d", &u, &v, &w);        sort(&u, &v);        add(v, u, -w);    }    int k=spfa();    if(k==0) {        printf("-1");        return 0;    }    if(dis[n]==inf) {        printf("-2");        return 0;    }    else printf("%d",dis[n]); return 0;}



0 0
原创粉丝点击