World Exhibition
发布日期:2021-07-14 01:03:52 浏览次数:54 分类:技术文章

本文共 3768 字,大约阅读时间需要 12 分钟。

传送门

描述

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
#include
#include
#include
#include
#include
#include
#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
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

转载地址:https://blog.csdn.net/cpongo3/article/details/89334364 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:欧拉函数性质、证明及求法
下一篇:Cornfields —二维RMQ

发表评论

最新留言

第一次来,支持一个
[***.219.124.196]2024年03月26日 05时17分37秒