World Exhibition

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

Total Submission(s): 1448    Accepted Submission(s): 715
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
 
Author
alpc20
 
Source
 
Recommend
zhouzeyong   |   We have carefully selected several similar problems for you:  1534 1529 3440 1531 3584 

题意:现有n个人,给你x+y个信息,前x条是说a--b之间最多相差c,

后y条信息说明a--b之间最少相差c,求1--n之间的最大距离,若距

离任意输出-1,如果不存在输出-2,否则就输出最大距离 

建图条件:(以1为源点)

1.dis[b]-dis[a]<=c

2.dis[b]-dis[a]>=c----dis[a]-dis[b]<=-c

3.dis[i+1]-dis[i]>=0----dis[i]-dis[i+1]<=0

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<queue>
#include<algorithm>
using namespace std;
#define MAXN 10010
#define MAXM 500000
#define INF 0x3f3f3f
int head[MAXN],vis[MAXN],dis[MAXN],used[MAXN];
int n,x,y,cnt;
struct node
{
int u,v,val;
int next;
}edge[MAXM];
void init()
{
memset(head,-1,sizeof(head));
cnt=0;
}
void add(int u,int v,int val)
{
edge[cnt].u=u;
edge[cnt].v=v;
edge[cnt].val=val;
edge[cnt].next=head[u];
head[u]=cnt++;
}
void getmap()
{
for(int i=1;i<n;i++)
add(i+1,i,0);
int a,b,c;
while(x--)
{
cin>>a>>b>>c;
add(a,b,c);
}
while(y--)
{
cin>>a>>b>>c;
add(b,a,-c);
}
}
void SPFA()
{
memset(vis,0,sizeof(vis));
memset(dis,INF,sizeof(dis));
dis[1]=0;
vis[1]=1;
used[1]++;
queue<int>q;
q.push(1);
while(!q.empty())
{
int u=q.front();
q.pop();
vis[u]=0;
for(int i=head[u];i!=-1;i=edge[i].next)
{
node E=edge[i];
if(dis[E.v]>dis[u]+E.val)
{
dis[E.v]=dis[u]+E.val;
if(!vis[E.v])
{
vis[E.v]=1;
used[E.v]++;
if(used[E.v]>n)
{
cout<<-1<<endl;
return ;
}
q.push(E.v);
}
}
}
}
if(dis[n]>=INF)
cout<<-2<<endl;
else
cout<<dis[n]<<endl;
}
int main()
{
int t;
cin>>t;
while(t--)
{
cin>>n>>x>>y;
init();
getmap();
SPFA();
}
return 0;
}

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

  1. HDOJ 1534 Schedule Problem 差分约束

    差分约数: 求满足不等式条件的尽量小的值---->求最长路---->a-b>=c----> b->a (c) Schedule Problem Time Limit: 2 ...

  2. hdu-3592 World Exhibition(差分约束)

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

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

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

  4. 【HDOJ】3592 World Exhibition

    基础差分约束. /* 3592 */ #include <iostream> #include <algorithm> #include <queue> #incl ...

  5. 差分约束 HDU - 1384 HDU - 3592 HDU - 1531 HDU - 3666

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

  6. POJ 3169 Layout (HDU 3592) 差分约束

    http://poj.org/problem?id=3169 http://acm.hdu.edu.cn/showproblem.php?pid=3592 题目大意: 一些母牛按序号排成一条直线.有两 ...

  7. HDOJ 1384 差分约束

    结题报告合集请戳:http://972169909-qq-com.iteye.com/blog/1185527 /*题意:求符合题意的最小集合的元素个数 题目要求的是求的最短路, 则对于 不等式 f( ...

  8. 图论--差分约束--HDU\HDOJ 4109 Instrction Arrangement

    Problem Description Ali has taken the Computer Organization and Architecture course this term. He le ...

  9. 【转】最短路&差分约束题集

    转自:http://blog.csdn.net/shahdza/article/details/7779273 最短路 [HDU] 1548 A strange lift基础最短路(或bfs)★254 ...

  10. 转载 - 最短路&差分约束题集

    出处:http://blog.csdn.net/shahdza/article/details/7779273 最短路 [HDU] 1548    A strange lift基础最短路(或bfs)★ ...

随机推荐

  1. pyspark MLlib踩坑之model predict+rdd map zip,zip使用尤其注意啊啊啊!

    Updated:use model broadcast, mappartition+flatmap,see: from pyspark import SparkContext import numpy ...

  2. 杂项-建模:BIM

    ylbtech-杂项-建模:BIM 建筑信息模型是建筑学.工程学及土木工程的新工具.建筑信息模型或建筑资讯模型一词由Autodesk所创的.它是来形容那些以三维图形为主.物件导向.建筑学有关的电脑辅助 ...

  3. P3376 【模板】网络最大流(70)

    题目描述 如题,给出一个网络图,以及其源点和汇点,求出其网络最大流. 输入输出格式 输入格式: 第一行包含四个正整数N.M.S.T,分别表示点的个数.有向边的个数.源点序号.汇点序号. 接下来M行每行 ...

  4. Android 给WebView设置Cookie

    最近项目中用到WebView访问新浪支付页面,有个要求是必须是登录状态,否则会报Token过期,然后我简单的将我从cookie中取得的ticket,即一串数字可以代表用户登录的唯一标识作为参数拼接到u ...

  5. 有关windows dpi适配(c#)

    /// <summary>当前Dpi</summary> public static Int32 Dpi { get; set; } /// <summary>修正 ...

  6. web开发必看:你的网站支持https吗?

    如果有一项技术可以让网站的访问速度更快.更安全.并且seo权重提升(百度除外),而且程序员不需要改代码就可以全站使用,最重要的是,不需要额外花钱,那有这么好的事情吗? HTTP通信协议是全球万维网ww ...

  7. bootstrap-导航条

    默认样式的导航条 导航条是在您的应用或网站中作为导航页头的响应式基础组件.它们在移动设备上可以折叠(并且可开可关),且在视口(viewport)宽度增加时逐渐变为水平展开模式. 两端对齐的导航条导航链 ...

  8. 陆、jq基础语法

    一.概述:更加方便的处理html文档.events事件.动画效果和ajax交互等. 1.jq主要功能: (1)访问页面框架的局部. (2)修改页面表现 (3)更改页面的内容 (4)响应事件 (5)为页 ...

  9. Settings Django Static Files

    静态文件是通过django.contrib.staticfiles来管理的. 配置Django静态文件,Djang官网静态文件配置介绍.简言之,通过以下三个步骤来配置和加载静态文件: 设置静态文件别名 ...

  10. BZOJ 4006 [JLOI2015]管道连接(斯坦纳树+子集DP)

    明显是一道斯坦纳树的题. 然而这题只需要属性相同的点互相连接. 我们还是照常先套路求出\(ans[s]\). 然后对\(ans[s]\)做子集DP即可. 具体看代码. #include<iost ...