HDU3592 World Exhibition

该博客讨论了一个关于世博会排队的问题,其中人们根据喜好关系需要保持特定距离。问题要求计算第一个人和最后一个人间能实现的最大距离,满足喜欢彼此的人之间不超过设定距离,不喜欢的人之间至少保持设定距离。通过构建约束图并寻找最短路径来解决此问题。如果存在负环,则表示无法满足条件。
摘要由CSDN通过智能技术生成
problem

Nowadays, many people want to go to Shanghai to visit the World Exhibition. So there are always a lot of people who are standing along a straight line waiting for entering. Assume that there are N (2 <= N <= 1,000) people numbered 1..N who are standing in the same order as they are numbered. It is possible that two or more person line up at exactly the same location in the condition that those visit it in a group.
There is something interesting. Some 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 X (1 <= X <= 10,000) constraints describes which person like each other and the maximum distance by which they may be separated; a subsequent list of Y constraints (1 <= Y <= 10,000) tells which person 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 person 1 and person N that satisfies the distance constraints.

Input

First line: An integer T represents the case of test.
The next line: Three space-separated integers: N, X, and Y.
The next X lines: Each line contains three space-separated positive integers: A, B, and C, with 1 <= A < B <= N. Person A and B must be at most C (1 <= C <= 1,000,000) apart.
The next Y lines: Each line contains three space-separated positive integers: A, B, and C, with 1 <= A < B <= C. Person A and B must be at least C (1 <= C <= 1,000,000) apart.

Output

For each line: A single integer. If no line-up is possible, output -1. If person 1 and N can be arbitrarily far apart, output -2. Otherwise output the greatest possible distance between person 1 and N.

题意

共有 N N 个人按1N的编号排队,给出 X X 组互相亲近的人,之间的距离不能超过 C,给出 Y Y 组互相厌恶的人,之间的距离不能小于C,问第 1 1 个人和第N个人之间的最大距离

题解

依题意
对于相互亲近的人有 dBdAC d B − d A ≤ C w(A,B)=C w ( A , B ) = C
对于相互厌恶的人有 dBdAC d B − d A ≥ C w(B,A)=C w ( B , A ) = − C
由于 N N 个人按1N的编号排队,则有 d[i+1]d[i]0 d [ i + 1 ] − d [ i ] ≥ 0 w(i+1,i)=0 w ( i + 1 , i ) = 0
对约束图求最短路即为解
若存在负环即无解

代码
#include<bits/stdc++.h>
using namespace std;

const int inf = 1e9;
const int maxn = 1100;

struct node
{
    int to,w;
    node (int i=0,int j=0)
    {
        to = i, w = j;
    }
};
int N,d[maxn],cnt[maxn];
bool vis[maxn];
vector <node> e[maxn];

bool spfa()
{
    memset(vis,0,sizeof(vis));
    memset(cnt,0,sizeof(cnt));
    for (int i=1;i<=N;i++) d[i] = inf;

    queue <int> Q;
    Q.push(1);
    d[1] = 0, vis[1] = 1;
    while (!Q.empty())
    {
        int u = Q.front();

        for (int i=0;i<e[u].size();i++)
        {
            int v = e[u][i].to;
            int dis = e[u][i].w;
            if (d[v] > d[u]+dis)
            {
                d[v] = d[u]+dis;
                if (!vis[v])
                {
                    vis[v] = 1;
                    Q.push(v);
                    cnt[v]++;
                    if (cnt[v] > N) return false;
                }
            }
        }

        Q.pop();
        vis[u] = 0;
    }
    return true;
}

int main()
{
    int T,X,Y,A,B,C;
    scanf("%d",&T);
    while (T--)
    {
        scanf("%d %d %d",&N,&X,&Y);

        for (int i=1;i<=N;i++) e[i].clear();

        for (int i=0;i<X;i++)
        {
            scanf("%d %d %d",&A,&B,&C);
            e[A].push_back(node(B,C));
        }
        for (int i=0;i<Y;i++)
        {
            scanf("%d %d %d",&A,&B,&C);
            e[B].push_back(node(A,-C));
        }
        for (int i=1;i<N;i++)
            e[i+1].push_back(node(i,0));

        if (spfa())
        {
            if (d[N] == inf) printf("-2\n");
            else
                printf("%d\n",d[N]);
        }
        else
            printf("-1\n");
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值