最短路练习14/poj/3169 Layout 差分约束spfa解法


题目链接:http://poj.org/problem?id=3169
Layout
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 11283 Accepted: 5421

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.

这道题我数组开小了,居然wa了,我的Re去哪了,害我又白交了30几遍才发现第三次交的的代码改下数组大小就能过。 抓狂

题意: 有n头牛,按照序号1……n排队,多头牛可以站在同一个位置(暂且这么认为),也就是任意两头牛之间的距离都大于等于0。先给出ml组约束关系(u,v,w)代表第u牛和第v牛之间的距离要小于等于w。再给出md组关系(u,v,w),代表第u牛和第v牛之间的距离要大于等于w。求这n头牛排成的队伍能否符合以上的约束,不能的话(也就是出现负环),输出“-1”,如果距离是inf,输出“-2”。否则输出这个最短距离。

 

思路:转化为差分约束模型。dis[i]表示第i头牛所在的位置。要注意除了题目所给出的ml+md条约束外还要加入任意两头牛之间距离大于等于0的约束,也就是dis[i+1]>dis[i]。把1点设为最短路始点,如果spfa判断出负环的话就输出-1,如果dis[n]==inf则输出-2。否则输出dis[n]。

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
#include<map>
#include<string>
#define LL long long
#define eps 1e-8
using namespace std;
const int mod = 1e7+7;
const int inf = 0x3f3f3f3f;
const int maxx = 200010;
const int N = 10500;
int n,ml,md,cnt=0;
struct node
{
    int y,v;
} p[maxx];
int head[N],next[N];
int dis[N],vis[N];
int q[maxx];
int num[N];
void add(int a,int b,int c)
{
    p[cnt].y=b;
    p[cnt].v=c;
    next[cnt]=head[a];
    head[a]=cnt++;
}
int relax(int a,int b,int c)
{
    if(dis[a]+c<dis[b])
    {
        dis[b]=dis[a]+c;
        return 1;
    }
    return 0;
}
int spfa()
{
    memset(dis,inf,sizeof(dis));
    memset(vis,0,sizeof(vis));
    memset(num,0,sizeof(num));
    dis[1]=0;
    vis[1]=1;
    num[1]++;
    int top=0,f,k;
    q[top++]=1;
    while(top)
    {
        f=q[--top];
        vis[f]=0;
        k=head[f];
        while(k!=-1)
        {
            if(relax(f,p[k].y,p[k].v)&&!vis[p[k].y])
            {
                vis[p[k].y]=1;
                q[top++]=p[k].y;
                num[p[k].y]++;
                if(num[p[k].y]>n)
                    return -1;
            }
            k=next[k];
        }
    }
    if(dis[n]==inf)
        return -2;
    return dis[n];
}
int main()
{
    int a,b,c;
    memset(head,-1,sizeof(head));
    scanf("%d%d%d",&n,&ml,&md);
    for(int i=0; i<ml; i++)
    {
        scanf("%d%d%d",&a,&b,&c);
        add(a,b,c);
    }
    for(int i=0; i<md; i++)
    {
        scanf("%d%d%d",&a,&b,&c);
        add(b,a,-c);
    }
        printf("%d",spfa());
}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值