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;
}

猜你喜欢

转载自blog.csdn.net/qq_41410799/article/details/81940544