World Exhibition

本文探讨了一种使用SPFA算法解决特定排队问题的方法,该问题涉及在一系列约束条件下,计算队伍首尾两人间可能的最大距离。通过构建图模型,并运用链式向前星结构,实现了对喜欢和不喜欢关系的距离约束的有效处理。

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

传送门 HDU3592

描述

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.

输入

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.

输出

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.

样例

  • Input
    1
    4 2 1
    1 3 8
    2 4 15
    2 3 4
  • Output
    19

思路

  • 题意:1~N人排队,X行中a和b最多距离c,Y行中a和b最少距离c,求最后1和N之间的最大距离
  • 题目求1到N的最大距离,则求图中1到N的最短路
  • 对于x,b-a<=c,a到b建权值为c的有向边;
    对于y,b-a>=c => a-b<=-c,b到a建权值-c的有向边;
    另外,对于第i个人,i+1 - i >=0 => i - i+1<=0,i+1到i建权值为0的有向边。
  • spfa 链式向前星

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include<iostream>
#include<stdio.h>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#define INIT(a,b) memset(a,b,sizeof(a))
#define LL long long int
using namespace std;
const int MAX=0x7fffffff;
const int MIN=-0x7fffffff;
const int INF=0x3f3f3f3f;
const int Mod=1e9+7;
const int maxn=1e4+7;
int Begin[maxn],used[maxn],dis[maxn],n,vis[maxn];
struct Node{int To,Dis,Next;};
Node Edge[100007];int top=0;
void add(int x,int y,int z){
Edge[top]=(Node){y,z,Begin[x]};
Begin[x]=top++;
}
int spfa(int t){
INIT(used,0);INIT(vis,0);
for(int i=0;i<=n;i++) dis[i]=INF;
dis[t]=0;
queue<int>road;
road.push(t);used[t]=1;vis[t]++;
while(!road.empty()){
int now=road.front();used[now]=0;road.pop();
for(int i=Begin[now];i!=-1;i=Edge[i].Next){
int ne=Edge[i].To;
if(dis[ne]>dis[now]+Edge[i].Dis){
dis[ne]=dis[now]+Edge[i].Dis;
if(!used[ne]){
used[ne]=1,road.push(ne);
vis[ne]++;
if(vis[ne]>=n)return -1;
}
}
}
}
if(dis[n]==INF)return -2;
return dis[n];
}
int main(){
//freopen("1.in","r",stdin);
//freopen("1.out","w",stdout);
int t,x,y;scanf("%d",&t);
while(t--){
INIT(Begin,-1);top=0;
scanf("%d%d%d",&n,&x,&y);
int a,b,c;
while(x--){
scanf("%d%d%d",&a,&b,&c);
add(a,b,c);
}
while(y--){
scanf("%d%d%d",&a,&b,&c);
add(b,a,-c);
}
for(int i=1;i<n;i++) add(i+1,i,0);
printf("%d\n",spfa(1));
}
return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值