POJ3169 Layout(差分约束)

本文介绍了一种使用SPFA算法解决特定场景下牛排队问题的方法,该问题要求根据牛之间的距离约束来确定牛之间的最大可能距离。通过构建图模型并应用SPFA算法寻找最短路径,解决了此问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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头奶牛按照编号从小到大站成一排等待喂食,两头牛可以站在同一个坐标点上,然而有些牛必须要和另一些牛的距离不能超过D,有一些牛和另一些牛的距离不能小于D。

用dis[i]表示第i头牛到第一头牛的距离

对于一些牛距离不超过D的:dis[j] - dis[i] <= D

对于一些牛距离不小于D的:dis[j] - dis[i] >= D


1.对于差分不等式,a - b <= c ,建一条 b 到 a 的权值为 c 的边,求的是最短路,得到的是最大值(本题求的就是最大值),对于不等式 a - b >= c ,建一条 b 到 a 的权值为 c 的边,求的是最长路,得到的是最小值。
2.如果有负环,那么无解。
3.如果dis[n]为inf,那么可以是任意解。

这题直接用SPFA就可以了。


#include<queue>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define endl '\n'
using namespace std;

const int inf = 1e9;
int n,ml,md;
int dis[1100],b[1100],cnt[1100];
struct edge
{
    int to,cost;
    edge(int a,int b)
    {
        to = a;
        cost = b;
    }
};
vector<edge> e[2200];

int SPFA(int s)
{
    memset(b,0,sizeof(b));
    memset(cnt,0,sizeof(cnt));
    queue<int> q;
    for(int i=1;i<=n;i++)
        dis[i] = inf;
    dis[s] = 0;
    q.push(s);
    b[s] = 1;
    while(!q.empty())
    {
        int t = q.front();
        q.pop();
        b[t] = 0;
        for(int i=0;i<e[t].size();i++)
        {
            int x = e[t][i].to;
            if(dis[x] > dis[t] + e[t][i].cost)
            {
                dis[x] = dis[t] + e[t][i].cost;
                if (b[x] == 0)
                {
                    q.push(x);
                    b[x] = 1;
                    cnt[x]++;
                    if(cnt[x] > n)
                        return -1;
                }
            }
        }
    }

    return dis[n];
}
int main(void)
{
    int i,j;
    while(scanf("%d%d%d",&n,&ml,&md)==3)
    {
        for(i=1;i<=n;i++)
            e[i].clear();
        for(i=1;i<=ml;i++)
        {
            int x,y,z;
            scanf("%d%d%d",&x,&y,&z);
            if(x > y)
                swap(x,y);
            e[x].push_back(edge(y,z));
        }
        for(i=1;i<=md;i++)
        {
            int x,y,z;
            scanf("%d%d%d",&x,&y,&z);
            if(x > y)
                swap(x,y);
            e[y].push_back(edge(x,-z));
        }
        int ans = SPFA(1);
        if(ans == -1)
            printf("-1\n");
        else if(ans == inf)
            printf("-2\n");
        else
            printf("%d\n",ans);
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值