POJ-3169 Layout( 差分约束 )

题目链接:http://poj.org/problem?id=3169

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

有N头牛排队,多头牛可以在同一点。现有几对相互喜好的牛,他们不能超过一定距离,有几对相互厌恶的牛,他们至少隔开一定距离。求1和N最远相距多少
差分约束,有两个约束条件(以下i<j)d[j]-d[i]<=s[k],d[j]-d[i]>=s[k],将后者转换成d[i]-d[j]<=-s[k],就可以由约束条件建一个有向图
-1表示存在负环,对应题目理解为没有一种排队方式满足约束条件,因为有负环存在会导致后面的牛“插队”
-2表示dis[N]==INF,对应题目理解为N的位置可以无限远

#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<map>
#include<cstdio>
#include<queue>
#include<stack>

using namespace std;

const int INF = 0x3f3f3f3f;
const int MAXN = 1005;
const int MAXM = 1000005;

struct Edge{
    int to, next ,val;
}edge[MAXM * 2];

int n, ml, md, cnt, h[MAXN], dis[MAXN], sum[MAXN];
bool vis[MAXN];

void sort( int & a, int & b ){
    if( a > b ){
        int temp = a;
        a = b;
        b = temp;
    }
}

void add( int a, int b, int c ){
    edge[cnt].to = b;
    edge[cnt].val = c;
    edge[cnt].next = h[a];
    h[a] = cnt++;
}

bool spfa(){
    memset( vis, false, sizeof( vis ) );
    memset( dis, INF, sizeof( dis ) );
    memset( sum, 0, sizeof( sum ) );
    dis[1] = 0;
    stack<int> Q;
    Q.push( 1 );
    vis[1] = true;
    sum[1]++;

    while( !Q.empty() ){
        int x = Q.top(); Q.pop(); vis[x] = false;
        for( int k = h[x]; k != 0; k = edge[k].next ){
            int y = edge[k].to;
            if( dis[y] > dis[x] + edge[k].val ){
                dis[y] = dis[x] + edge[k].val;
                if( !vis[y] ){
                    Q.push( y );
                    vis[y] = true;
                    sum[y]++;
                    if( sum[y] > n ) return false;
                }
            }
        }
    }

    return true;
}

int main(){
    ios::sync_with_stdio( false );

    memset( h, 0, sizeof( h ) );
    cnt = 1;

    int a, b, c;
    cin >> n >> ml >> md;
    while( ml-- ){
        cin >> a >> b >> c;
        sort( a, b );
        add( a, b, c );
    }
    while( md-- ){
        cin >> a >> b >> c;
        sort( a, b );
        add( b, a, -c );
    }
 
    if( !spfa() ) cout << -1;
    else if( dis[n] == INF ) cout << -2;
    else cout << dis[n] << endl;
}
原文地址:https://www.cnblogs.com/hollowstory/p/5677078.html