题目链接:

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. 更改App名称

    To change the installed application name, in Xcode: 1. Select your Target on the left side under Gro ...

  2. Codevs1062路由选择

    /* #include<iostream> #include<cstdio> #include<cstring> #define MAXN 301 using na ...

  3. IntelliJ IDEA常用统一设置(Linux/Mac/Windows)

    前言:如果说VS是宇宙超级无敌第一大开发工具,那么IDEA是当之无愧的第二大开发工具,将来有机会把VS干掉. 说明:除了以下说明的配置地方外,其它尽量保持默认,这样有利于团队代码风格的统一. 运行VM ...

  4. C#使用PrintDocument打印 多页 打印预览

    PrintDocument实例所有的订阅事件如下: 创建一个PrintDocument的实例.如下: System.Drawing.Printing.PrintDocument docToPrint ...

  5. Nuxt.js使用lazyload

    Vue的使用方式: 1. 安装插件: npm install vue-lazyload --save-dev 2. main.js引入插件: import VueLazyLoad from 'vue- ...

  6. 如何使用RDP跳过网络隔离?

    简介 本文我将向大家演示,如何通过RDP跳转盒进入隔离/受保护的网络.下图是我为该场景制作的拓扑图: 简要说明: LAN是一种扁平的工作站和服务器网络. 一些服务器(包括RDP跳转盒)无法与Inter ...

  7. [转]Linux上程序执行的入口--Main

    main()函数,想必大家都不陌生了,从刚开始写程序的时候,大家便开始写main(),我们都知道main是程序的入口.那main作为一个函数,又是谁调用的它,它是怎么被调用的,返回给谁,返回的又是什么 ...

  8. maven最小配置

    将参与项目开发的开发人员的用户名及邮箱捆绑在一起,在code review是更加方便的进行版本管控: 1.配置user,name和user,email命令: $ git config --global ...

  9. linux下ndk编译命令行程序及配置

    1.在http://developer.android.com/tools/sdk/ndk/index.html下载Android-ndk-r8e-linux-x86.tar.bz2,解压后把andr ...

  10. requirejs中的define

    关于requirejs中的define的原理理解   我们已经了解到模块模式是为单例创建私有变量和特权方法的.一个最基本的例子: var foo=(function(){ var something= ...