World Exhibition

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1754    Accepted Submission(s): 886

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

 
差分约束系统
建图:
问题询问最大值,因此差分约束求最短路。不等式全部转化成 <= 号。
对于 dis[v] - dis[u] <= w  (u < v),从u到v建立一条权值为w的有向边。
对于 dis[v] - dis[u] >= w  (u < v), 将不等式转换为dis[u] - dis[v] <= -w  (u < v),从v到u建立一条权值为-w的有向边。
 
spfa找最短路。
  1. //2017-08-29
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <iostream>
  5. #include <algorithm>
  6. #include <queue>
  7. #include <stack>
  8.  
  9. using namespace std;
  10.  
  11. const int N = ;
  12. const int M = ;
  13. const int INF = 0x3f3f3f3f;
  14.  
  15. int head[N], tot;
  16. struct Edge{
  17. int to, next, w;
  18. }edge[M];
  19.  
  20. void init(){
  21. tot = ;
  22. memset(head, -, sizeof(head));
  23. }
  24.  
  25. void add_edge(int u, int v, int w){
  26. edge[tot].w = w;
  27. edge[tot].to = v;
  28. edge[tot].next = head[u];
  29. head[u] = tot++;
  30. }
  31.  
  32. int n, m, c;
  33. bool vis[N];
  34. int dis[N], cnt[N];
  35.  
  36. bool spfa(int s, int n){
  37. memset(vis, , sizeof(vis));
  38. memset(dis, INF, sizeof(dis));
  39. memset(cnt, , sizeof(cnt));
  40. vis[s] = ;
  41. dis[s] = ;
  42. cnt[s] = ;
  43. deque<int> dq;
  44. dq.push_back(s);
  45. int sum = , len = ;
  46. while(!dq.empty()){
  47. // LLL 优化
  48. while(dis[dq.front()]*len > sum){
  49. dq.push_back(dq.front());
  50. dq.pop_front();
  51. }
  52. int u = dq.front();
  53. sum -= dis[u];
  54. len--;
  55. dq.pop_front();
  56. vis[u] = ;
  57. for(int i = head[u]; i != -; i = edge[i].next){
  58. int v = edge[i].to;
  59. if(dis[v] > dis[u] + edge[i].w){
  60. dis[v] = dis[u] + edge[i].w;
  61. if(!vis[v]){
  62. vis[v] = ;
  63. // SLF 优化
  64. if(!dq.empty() && dis[v] < dis[dq.front()])
  65. dq.push_front(v);
  66. else dq.push_back(v);
  67. sum += dis[v];
  68. len++;
  69. if(++cnt[v] > n)return false;
  70. }
  71. }
  72. }
  73. }
  74. return true;
  75. }
  76.  
  77. int main()
  78. {
  79. std::ios::sync_with_stdio(false);
  80. //freopen("input.txt", "r", stdin);
  81. int T, n, x, y;
  82. cin>>T;
  83. while(T--){
  84. init();
  85. cin>>n>>x>>y;
  86. int u, v, w;
  87. while(x--){
  88. cin>>u>>v>>w;
  89. add_edge(u, v, w);
  90. }
  91. while(y--){
  92. cin>>u>>v>>w;
  93. add_edge(v, u, -w);
  94. }
  95. if(spfa(, n)){
  96. if(dis[n] == INF)cout<<-<<endl;
  97. else cout<<dis[n]<<endl;
  98. }else cout<<-<<endl;
  99. }
  100.  
  101. return ;
  102. }

HDU3592(差分约束)的更多相关文章

  1. poj 3169&hdu3592(差分约束)

    Layout Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9687   Accepted: 4647 Descriptio ...

  2. hdu3592(差分约束) (线性)

    题意:一些牛按序号排成一条直线,有两种要求,A和B距离不得超过X,还有一种是A和B距离不得少于Y,问1和N可能的最大距离. 和poj那题一样,就是多了多组数据. #include<cstring ...

  3. Candies-POJ3159差分约束

    Time Limit: 1500MS Memory Limit: 131072K Description During the kindergarten days, flymouse was the ...

  4. poj3159 差分约束 spfa

    //Accepted 2692 KB 1282 ms //差分约束 -->最短路 //TLE到死,加了输入挂,手写queue #include <cstdio> #include & ...

  5. ZOJ 2770火烧连营——差分约束

    偶尔做了一下差分约束. 题目大意:给出n个军营,每个军营最多有ci个士兵,且[ai,bi]之间至少有ki个士兵,问最少有多少士兵. ---------------------------------- ...

  6. POJ 2983 Is the Information Reliable? 差分约束

    裸差分约束. //#pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #i ...

  7. 2014 Super Training #6 B Launching the Spacecraft --差分约束

    原题:ZOJ 3668 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3668 典型差分约束题. 将sum[0] ~ sum ...

  8. POJ 1364 King --差分约束第一题

    题意:求给定的一组不等式是否有解,不等式要么是:SUM(Xi) (a<=i<=b) > k (1) 要么是 SUM(Xi) (a<=i<=b) < k (2) 分析 ...

  9. [USACO2005][POJ3169]Layout(差分约束)

    题目:http://poj.org/problem?id=3169 题意:给你一组不等式了,求满足的最小解 分析: 裸裸的差分约束. 总结一下差分约束: 1.“求最大值”:写成"<=& ...

随机推荐

  1. [LNOI2014]LCA(树剖+线段树)

    \(\%\%\% Fading\) 此题是他第一道黑题(我的第一道黑题是蒲公英) 一直不敢开,后来发现是差分一下,将询问离线,树剖+线段树维护即可 \(Code\ Below:\) #include ...

  2. 01-Linux的基本指令

    Linux的基本指令 基础指令(重点) 1.ls指令: 含义:ls(list) 用法1:#ls 含义:列出当前工作目录下的所有文件/文件夹的名称 用法2:#ls  路径 含义:列出指定路径下的所有文件 ...

  3. Vue2.5开发去哪儿网App 城市列表开发之 兄弟组件间联动及列表性能优化

    一,  兄弟组件间联动 1.  点击城市字母,左侧对应显示 给遍历的 字母 添加一个点击事件: Alphabet.vue @click="handleLetterClick" ha ...

  4. 【tomcat】servlet原理及其生命周期

    1.什么是servlet? Servlet(Servlet Applet),全称Java Servlet,是用Java编写的服务器端程序.而这些Servlet都要实现Servlet这个接口.其主要功能 ...

  5. odoo开发笔记-日期时间相关操作

    日期格式化字符串:DATE_FORMAT = "%Y-%m-%d" 日期时间格式字符串:DATETIME_FORMAT = "%Y-%m-%d %H:%M:%S" ...

  6. copy代码的时候,如何去掉代码前边的编号

    从网页上拷贝下来的代码前面总有编号,如何去掉! 1.使用正则表达式:在editorplus(notepad++)里按ctrl+h,弹出框里勾选上“正则表达式(regular expression)”, ...

  7. 课程一(Neural Networks and Deep Learning),第一周(Introduction to Deep Learning)—— 0、学习目标

    1. Understand the major trends driving the rise of deep learning.2. Be able to explain how deep lear ...

  8. c++的动态绑定和静态绑定

    为了支持c++的多态性,才用了动态绑定和静态绑定. 1.对象的静态类型:对象在声明时采用的类型.是在编译期确定的. 2.对象的动态类型:目前所指对象的声明.在运行期决定.对象的动态类型可以更改,但是静 ...

  9. 03-03:springBoot 整合thymeleaf

    thymeleaf 语法详解1.变量输出: th:text :在页面中输出某个值 th:value :将一个值放到input标签中的value中.2.判断字符串是否为空 ①:调用内置对象一定要用# ② ...

  10. Python爬取简书主页信息

    主要学习如何通过抓包工具分析简书的Ajax加载,有时间再写一个Multithread proxy spider提升效率. 1. 关键点: 使用单线程爬取,未登录,爬取简书主页Ajax加载的内容.主要有 ...