题目链接:

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. Python入门--7--处理数据时学习到的东西

    一.数据导入(这里使用的是pands包) import pands as pd wenjian = pd.read_csv('路径') 二.数据变换 print wenjian.head()    # ...

  2. ci框架——修改分页的显示样式

    修改ci框架分页的显示样式 用过ci框架的都知道,ci框架自带的分页样式是1,2下一页,在最开始刷新页面现实的时候如果页面不够多的话,那么首页和末页是不显的,这是ci框架的一个缺点, 这个时候需要我们 ...

  3. ls 不是内部或外部命令

    在C:\windows目录下新建一个文件 命名为 ls.bat 打开编辑这个文件 输入: @echo off dir 这两句保存即可.

  4. 详解 CSS 七种三栏布局技巧

    作者:林东洲 链接:https://zhuanlan.zhihu.com/p/25070186 来源:知乎 著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 三栏布局,顾名思义就是 ...

  5. java验证身份证号码是否有效源代码 wn25的头像 wn25 23 2015-01-04 20:09 6 基本信息 Java × 1 浏览

    原文:http://www.open-open.com/code/view/1420373343171 1.描述 用java语言判断身份证号码是否有效,地区码.出身年月.校验码等验证算法 2.源代码 ...

  6. Linux索引节点(Inode:no space for device)用满导致的一次故障

    问题描写叙述 在storm測试环境集群上上nimbus和supervisor自己主动挂调.重新启动时显示no space for device,也不能创建,加入文件及文件夹,df -h查看 ilesy ...

  7. 机器学习技法总结(六)Decision Tree Hypothesis

    这里先再次提出我们利用aggregation获取更好性能的Hypothesis G所涉及的方法:blending,就是在得到g_set之后进行融合:learning呢?就是在线online的获取g并融 ...

  8. html5 式程序员表白

    html5 式程序员表白 王海庆 于 星期三, 04/06/2014 - 00:44 提交 今天是个好日子,2014年5月20日,表白的最佳时机,虽说孩子已经四岁.结婚已经五年.可是也不能够偷懒.于是 ...

  9. chrome.declarativeWebRequest

    chrome.declarativeWebRequest 清单文件 规则 条件与操作的求值 使用优先级覆盖规则 类型 HeaderFilter RequestMatcher CancelRequest ...

  10. Simple calculations

    Description 有一个包括n+2个元素的数列a0, a1, ..., an+1 (n <= 3000, -1000 <= ai <=1000).它们之间满足ai = (ai- ...