题目链接:

World Exhibition

Time Limit: 2000/1000 MS (Java/Others)   

 Memory Limit: 32768/32768 K (Java/Others)

Problem Description
 
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.
 
Sample Input
 
1
4 2 1
1 3 8
2 4 15
2 3 4
 
Sample Output
 
19
 
 
题意:
 
n个人站成一行,有两个人之间的距离最少是多少,有两个人之间的距离最大是多少,问第一个人到第 n个人之间的距离最大是多少;
 
思路:
 
由题意可以得到这样的不等式:
X行:
dis[B]-dis[A]<=C;
dis[A]-dis[B]<=0;
Y行:
dis[A]-dis[B]<=-C;
 
这是转化成最短路径的写法,当然也可以转化成求最长路径;
当无法满足的时候就是-1,当1和n不连通的时候就是-2;否则就是dis[n]了;
用spfa算法好快;
 
AC代码:
//#include <bits/stdc++.h>
#include <iostream>
#include <queue>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std;
#define Riep(n) for(int i=1;i<=n;i++)
#define Riop(n) for(int i=0;i<n;i++)
#define Rjep(n) for(int j=1;j<=n;j++)
#define Rjop(n) for(int j=0;j<n;j++)
#define mst(ss,b) memset(ss,b,sizeof(ss));
typedef long long LL;
const LL mod=1e9+;
const double PI=acos(-1.0);
const int inf=0x3f3f3f3f;
const int N=2e4+;
int cnt,n,x,y,vis[N],head[N],num[N];
int dis[N];
struct Edge
{
int to,next,val;
}edge[*N];
void add_edge(int s,int e,int val)
{
edge[cnt].to=e;
edge[cnt].next=head[s];
edge[cnt].val=val;
head[s]=cnt++;
}
queue<int>qu;
void spfa()
{
while(!qu.empty())qu.pop();
mst(vis,);
mst(dis,inf);
mst(num,);
qu.push();
dis[]=;
vis[]=;
while(!qu.empty())
{
int fr=qu.front();
qu.pop(); num[fr]++;
if(num[fr]>n)
{
printf("-1\n");
return ;
}
for(int i=head[fr];i!=-;i=edge[i].next)
{
int x=edge[i].to;
if(dis[x]>dis[fr]+edge[i].val)
{
dis[x]=dis[fr]+edge[i].val;
if(!vis[x])
{
qu.push(x);
vis[x]=;
}
}
}
vis[fr]=;
}
if(dis[n]==inf)printf("-2\n");
else printf("%d\n",dis[n]);
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
cnt=;
mst(head,-);
int u,v,w;
scanf("%d%d%d",&n,&x,&y);
Riep(x)
{
scanf("%d%d%d",&u,&v,&w);
add_edge(u,v,w);
add_edge(v,u,);
}
Riep(y)
{
scanf("%d%d%d",&u,&v,&w);
add_edge(v,u,-w);
}
spfa();
} return ;
}
 

hdu-3592 World Exhibition(差分约束)的更多相关文章

  1. hdu 1534 Schedule Problem (差分约束)

    Schedule Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  2. HDU 1384 Intervals【差分约束-SPFA】

    类型:给出一些形如a−b<=k的不等式(或a−b>=k或a−b<k或a−b>k等),问是否有解[是否有负环]或求差的极值[最短/长路径].例子:b−a<=k1,c−b&l ...

  3. hdu 1531 king(差分约束)

    King Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  4. hdu 1384 Intervals (差分约束)

    Problem - 1384 好歹用了一天,也算是看懂了差分约束的原理,做出第一条查分约束了. 题意是告诉你一些区间中最少有多少元素,最少需要多少个元素才能满足所有要求. 构图的方法是,(a)-> ...

  5. HDU 3592 World Exhibition(线性差分约束,spfa跑最短路+判断负环)

    World Exhibition Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  6. HDU 3592 World Exhibition (差分约束,spfa,水)

    题意: 有n个人在排队,按照前后顺序编号为1~n,现在对其中某两人的距离进行约束,有上限和下限,表示dis[a,b]<=c或者dis[a,b]>=c,问第1个人与第n个人的距离最多可能为多 ...

  7. HDU.1529.Cashier Employment(差分约束 最长路SPFA)

    题目链接 \(Description\) 给定一天24h 每小时需要的员工数量Ri,有n个员工,已知每个员工开始工作的时间ti(ti∈[0,23]),每个员工会连续工作8h. 问能否满足一天的需求.若 ...

  8. HDU 1384 Intervals(差分约束)

    Intervals Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  9. hdu3592 World Exhibition --- 差分约束

    这题建图没什么特别 x个条件:Sb-Sa<=c y个条件:Sa-Sb<=-c 题目问的是.1和n之间的关系. 有负环的话,整个就不可能成立,输出-1 假设图是连通的(1到n是连通的),就输 ...

随机推荐

  1. Mac OS X 10.10.5 中 VirtualBox 5.0 里的 Win7 虚拟机无法使用 USB 3.0 设备的解决办法(补充说明)

    上一篇文章中,我说到了如何在Mac OS X 10.10.5 中让 VirtualBox 5.0 里的 Win7 虚拟机使用 USB 3.0.最近碰巧升级 MacBook Pro 为最新的 15 吋 ...

  2. T9270 mjt树

    题目背景 从前森林里有一棵很大的mjt树,树上有很多小动物. 题目描述 mjt树上有 n 个房间,第 i 个房间住着 ai 只第bi 种小动物. 这n个房间用n-1条路连接起来,其中房间1位mjt树的 ...

  3. LinkedList类的基本方法的用法

    package cn.zmh.LinkedList; import java.util.Iterator; import java.util.LinkedList; public class Link ...

  4. 基于SSH+shiro+solr的家庭记账系统

    项目地址: https://github.com/jianghuxiaoao/homeaccount

  5. (转) go Cron的使用

    Cron表达式是一个字符串,字符串以5或6个空格隔开,分为6或7个域,每一个域代表一个含义,Cron有如下两种语法格式: Seconds Minutes Hours DayofMonth Month ...

  6. html页面中拍照和上传照片那些事儿(二)

    本文为原创,转载请注明出处: cnzt       文章:cnzt-p http://www.cnblogs.com/zt-blog/p/6895352.html 本文主要说下iOS上传的照片在安卓机 ...

  7. 【Todo】ssh的原理和实践

    有空的时候补充,可以参考 http://www.ruanyifeng.com/blog/2011/12/ssh_remote_login.html http://www.ruanyifeng.com/ ...

  8. vim 精确匹配查找单词【转】

    删除文件中所有的空行:g/^\s*$/d 去掉所有的行尾空格::%s/\s\+$// 整个文件特定字符串的替换:%s/old_word/new_word/g 删除从当前行开始到最后一行的所有内容:., ...

  9. BUPT复试专题—Special 数(2017)

    题目描述 设一个正整数既是平方数乂是立方数时,称为Special数. 输入 输入包含多组测试数据,笫1行输入测试数据的组数,接下来在后续每行输入n(n<=1000000000) 输出 输出1到n ...

  10. [转]Go基础之锁的初识

    当我们的程序就一个线程的时候是不需要用到锁的,但是通常我们实际的代码不会是单个线程的,所有这个时候就需要用到锁了,那么关于锁的使用场景主要涉及到哪些呢? 当我们多个线程在读相同的数据的时候则是需要加锁 ...