1. 程式人生 > >POJ 3169 Layout 差分約束+spfa

POJ 3169 Layout 差分約束+spfa

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

題意:

給出兩點之間的最大或者最小距離,求1-N的最小距離, 如果可能有最小距離的話, 求最小距離, 如果不能有確定的最小距離的話輸出-1, 如何沒有最小距離的話, 輸出-2;

我們根據題意可以求出如下關係式:

d[B]-d[A]<=D; d[B]-d[A]>=D;

前者我們通過轉換可以轉換成下列式子:

d[B]<=D+d[A];

後面的我們可以轉換成如下式子:

d[A]<=d[B]-D;

然後我們再用spfa進行求解。

怎麼判斷是否有確定的最小距離呢,如果最短路出現負環的話,那麼肯定沒有確定的最小距離,輸出-2。

如果N根本不能與1點相通的話輸出-1.

程式碼如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
const int maxn=1005;
const int INF=0x3f3f3f3f;
int n,ml,md;
struct edge
{
    int e;
    int len;
};
vector <edge> ve[maxn];
int d[maxn];
int cnt[maxn];
void init ()
{
    d[1]=0;
    for (int i=2;i<=n;i++)
         d[i]=INF;
    memset (cnt,0,sizeof(cnt));
}
void spfa ()
{
    queue<int> q;
    q.push(1);
    while (!q.empty())
    {
        int now=q.front(); q.pop();
        for (int i=0;i<ve[now].size();i++)
        {
            int v=ve[now][i].e;
            //求最小邊
            if(d[v]>d[now]+ve[now][i].len)
            {
                d[v]=d[now]+ve[now][i].len;
                q.push(v);
                cnt[v]++;
                if(cnt[v]>n)
                {
                    printf("-1\n");
                    return;
                }
            }
        }
    }
    if(d[n]==INF)
    {
        printf("-2\n");
        return ;
    }
    printf("%d\n",d[n]);
}
int main()
{
    scanf("%d%d%d",&n,&ml,&md);
    init();
    int x,y,len;
    edge temp;
    for (int i=0;i<ml;i++)
    {
        scanf("%d%d%d",&x,&y,&len);
        temp.e=y; temp.len=len;
        ve[x].push_back(temp);
    }
    for (int i=0;i<md;i++)
    {
        scanf("%d%d%d",&x,&y,&len);
        temp.e=x; temp.len=-len;
        ve[y].push_back(temp);
    }
    spfa();
    return 0;
}