题目链接:

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. [转]UITableView全面解析

      转自:http://www.cnblogs.com/kenshincui/p/3931948.html#mvc 概述 在iOS开发中UITableView可以说是使用最广泛的控件,我们平时使用的软 ...

  2. awk 对简单文本处理试水

    #juanjuan是一个文件 [root@localhost c_test]# cat juanjuan , , bffd97d0 , , bffd97cc , , bffd97c8 , , #-F ...

  3. Windows下,RabbitMQ安装、卸载以及遇到的坑

    RabbitMQ是目前比较使用比较广泛的一个队列服务器,但是很多朋友在使用过程中,也遇到一些问题,这篇文章主要是做一个总结吧 本篇文章,虽然标题命名为“安装与卸载”,但是网上有很多类似的文章,我就简单 ...

  4. OSI模型详解

    OSI 七层模型通过七个层次化的结构模型使不同的系统不同的网络之间实现可靠的通讯,因此其最主要的功能就是帮助不同类型的主机实现数据传输 . 完成中继功能的节点通常称为中继系统.在OSI七层模型中,处于 ...

  5. Java下接口interface前面要不要加I

    说明:加I和不加I都可以,看需要,没有强制要求. 在Java中更多是提倡不加I的,可以看下JDK的源码,都是不加I的. 微软C#是规定要加I,这也是影响从而导致有这个话题的原因. Java中特定不直接 ...

  6. ipython结合virtualenv使用

    1.virtualenv使python的开发环境相互隔离,隔离环境可以安装自己的依赖包,避免冲突 2.ipython是交互使用python变的便利 3.在virtualenv环境里使用ipython即 ...

  7. UltraEdit UE常见问题 使用必读

    1 UE设置出多个tag标签,能切换的那种样子 现在的现象就是图上显示的样子,我想弄出来多个标签能切换的样子,就像浏览器的多个标签那样,怎么操作呢 视图->视图/列表->打开文件标签可以使 ...

  8. [LeetCode][Java] Container With Most Water

    题目: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, a ...

  9. 怎样查询锁表的SQL

    通过以下的语句查询出锁表的SQL: select l.session_id sid, s.serial#,        l.locked_mode,        l.oracle_username ...

  10. 让Quality Center走下神坛--测试管理工具大PK(转)

    让Quality Center走下神坛--测试管理工具QC/ALM 和 RQM.Jira.TP.SCTM大PK 在写完了<让QTP走下神坛>之后,现在来谈谈测试管理工具,献给所有正在或打算 ...