POJ 3169 Layout

题目链接
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、ml、md,表示有n个点,接下来ml行,每行三个数a、b、c,表示ab之间最远距离是不能超过c,之后是md行,每行三个数a、b、c表示ab之间最近距离不能小于c,问1和n之间的点距离最大是多少,如果没有限制则输出-1,如果没有解则输出-2

具体思路
这可能是我写过最规范的一次最短路了,链式前向星+优先队列+spfa负环,思路其实很简单,就是差分约束原理,将式子a-b<=c进行建边,对于最近的距离a-b>=c只要左右添一个负号变成b-a<=-c就是和上面一致的式子了,因为存在负环所以用spfa来跑最短路,最后输出距离即可,如果过程中出现负环就是所有的牛没有解,输出-1,如果两点距离是无穷大说明其没有限制,输出-2

#include<stdio.h>
#include<iostream>
#include<queue>
#include<string.h>
#include<stack>
#include<math.h>
using namespace std;
typedef long long int LLD;
const LLD linenum=20050,potnum=1050,inf=0x7fffffff;
LLD head[potnum],dis[potnum],flag[potnum],flagnum[potnum],linesum;
struct node
{
    LLD from,to,cost,pre;
}line[linenum];
struct pnode
{
    LLD from;
    LLD cost;
    bool operator < (const pnode &pre) const
    {
        return cost>pre.cost;
    }
};
void init(LLD n)
{
    linesum=0;
    for (LLD i=1;i<=n;i++)
    {
        head[i]=-1;
        dis[i]=inf;
        flag[i]=0;
        flagnum[i]=0;
    }
    return ;
}
void addline(LLD from,LLD to,LLD cost)
{
    linesum++;
    line[linesum].from=from;
    line[linesum].to=to;
    line[linesum].cost=cost;
    line[linesum].pre=head[from];
    head[from]=linesum;
    return ;
}
LLD spfa(LLD from,LLD to)
{
    priority_queue<pnode>pdui;
    pdui.push({from,0});
    dis[from]=0;
    flag[from]=1;
    while(!pdui.empty())
    {
        pnode now=pdui.top();
        pdui.pop();
        flag[now.from]=0;
        for (LLD i=head[now.from];i!=-1;i=line[i].pre)
        {
            node next=line[i];
            if (dis[next.to]>dis[next.from]+next.cost)
            {
                dis[next.to]=dis[next.from]+next.cost;
                if (!flag[next.to])
                {
                    flag[next.to]=1;
                    pdui.push({next.to,dis[next.to]});
                    flagnum[next.to]++;
                    if (flagnum[next.to]>to)
                    {
                        return -1;
                    }
                }
            }
        }
    }
    if (dis[to]==inf)
    {
        return -2;
    }else
    {
        return dis[to];
    }
}
int main()
{
    LLD n,ml,md;
    scanf("%lld %lld %lld",&n,&ml,&md);
    init(n);
    for (LLD i=1;i<=ml;i++)
    {
        LLD from,to,cost;
        scanf("%lld %lld %lld",&from,&to,&cost);
        addline(from,to,cost);
    }
    for (LLD i=1;i<=md;i++)
    {
        LLD from,to,cost;
        scanf("%lld %lld %lld",&from,&to,&cost);
        addline(to,from,-cost);
    }
    LLD ans=spfa(1,n);
    printf("%lld\n",ans);
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值