poj3169-Layout(差分约束+SPFA+判负环)

版权声明:转载请告知博主并要注明出处嗷~ https://blog.csdn.net/Akatsuki__Itachi/article/details/81736259

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.

题意:有N头牛,按照顺序排成一列。有的牛彼此喜欢~有的牛彼此厌恶。所以彼此喜欢的牛就想尽可能挨得近些,即他们的最大距离不能超过d1,彼此厌恶的牛就想挨得远些,最近距离不能低于d2,给出这种条件的牛的距离, 问第一头牛和最后一头牛的最大距离可能是多少?如果距离无限大输出-2,距离不存在(存在环)输出-1

当然,有些牛可能会在同一位置

既然是求最大距离,即求的是最短路。

设d[i]为第 i 头牛的位置,那么由给出的条件可以得到不等式

彼此喜欢的牛:d[b]-d[a]<=d1

彼此厌恶的牛:   d[b]-d[a]>=d1

隐藏的不等式:因为牛是按照顺序顺序排列,所以相邻的两头牛d[i+1]-d[i]>=0

鉴于求的是最短路,所以我们统一化简成xi - xj <= d 的形式,然后构造一条 j->i 的有向边

即:d[b]-d[a]<=d1 ; d[a]-d[b]<= -d1 ; d[i+1]-d[i]<=0

然后跑一遍SPFA即可。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<iomanip>
#include<algorithm>
#include<vector>
#include<queue>
#define max(a,b)   (a>b?a:b)
#define min(a,b)   (a<b?a:b)
#define swap(a,b)  (a=a+b,b=a-b,a=a-b)
#define memset(a,v)  memset(a,v,sizeof(a))
#define X (sqrt(5)+1)/2.0  //Wythoff
#define Pi acos(-1)
#define e  2.718281828459045
#define eps 1.0e-8
using namespace std;
typedef long long int LL;
typedef pair<int,int>pa;
const int MAXL(1e6);
const int INF(0x3f3f3f3f);
const int mod(1e9+7);
int dir[4][2]= {{-1,0},{1,0},{0,1},{0,-1}};
struct node
{
    int to,next,w;

} edge[MAXL+50];
int head[MAXL+50];
int vis[MAXL+50];
int dis[MAXL+50];
int num[MAXL+50];
int cnt=0;
int A,B,D;
void addedge(int x,int y,int w)
{
    edge[cnt].to=y;
    edge[cnt].next=head[x];
    edge[cnt].w=w;
    head[x]=cnt++;
}
int SPFA(int u)
{
    queue<int>q;
    memset(vis,0);
    memset(dis,INF);
    vis[u]=1;
    dis[u]=0;
    q.push(u);
    num[u]++;
    while(!q.empty())
    {
        int v=q.front();
        q.pop();
        vis[v]=0;
        for(int i=head[v]; ~i; i=edge[i].next)
        {
            int x=edge[i].to;
            if(dis[x]>dis[v]+edge[i].w)
            {
                dis[x]=dis[v]+edge[i].w;
                if(!vis[x])
                {
                    vis[x]=1;
                    q.push(x);
                    num[x]++;
                    if(num[x]>A)
                        return -1;
                }
            }
        }

    }
    return dis[A]==INF? -2:dis[A];
}

void init()
{
    cnt=0;
    memset(head,-1);
    for(int i=1; i<=B; i++)
    {
        int x,y,z;
        scanf("%d%d%d",&x,&y,&z);
        if(x>y) swap(x,y);
        addedge(x,y,z);
    }
    for(int i=1; i<=D; i++)
    {
        int x,y,z;
        scanf("%d%d%d",&x,&y,&z);
        if(x>y) swap(x,y);
        addedge(y,x,-z);
    }
    for(int i=1; i<=A; i++)
        addedge(i+1,i,0);
}
int main()
{
    while(~scanf("%d%d%d",&A,&B,&D))
    {
        init();
        int ans=SPFA(1);
        cout<<ans<<endl;
    }
}

猜你喜欢

转载自blog.csdn.net/Akatsuki__Itachi/article/details/81736259
今日推荐