Like everyone else, cows like to stand close to their friends when queuing for feed. FJ has N (2 <= N <= 1,000) cows numbered 1..N standing along a straight line waiting for feed. The cows are standing in the same order as they are numbered, and since they can be rather pushy, it is possible that two or more cows can line up at exactly the same location (that is, if we think of each cow as being located at some coordinate on a number line, then it is possible for two or more cows to share the same coordinate).


Some cows 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 ML (1 <= ML <= 10,000) constraints describes which cows like each other and the maximum distance by which they may be separated; a subsequent list of MD constraints (1 <= MD <= 10,000) tells which cows 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 cow 1 and cow N that satisfies the distance constraints.

Input

Line 1: Three space-separated integers: N, ML, and MD.



Lines 2..ML+1: Each line contains three space-separated positive integers: A, B, and D, with 1 <= A < B <= N. Cows A and B must be at most D (1 <= D <= 1,000,000) apart.



Lines ML+2..ML+MD+1: Each line contains three space-separated positive integers: A, B, and D, with 1 <= A < B <= N. Cows A and B must be at least D (1 <= D <= 1,000,000) apart.

Output

Line 1: A single integer. If no line-up is possible, output -1. If cows 1 and N can be arbitrarily far apart, output -2. Otherwise output the greatest possible distance between cows 1 and N.

Sample Input

4 2 1
1 3 10
2 4 20
2 3 3

Sample Output

27

Hint

Explanation of the sample:


There are 4 cows. Cows #1 and #3 must be no more than 10 units apart, cows #2 and #4 must be no more than 20 units apart, and cows #2 and #3 dislike each other and must be no fewer than 3 units apart.



The best layout, in terms of coordinates on a number line, is to put cow #1 at 0, cow #2 at 7, cow #3 at 10, and cow #4 at 27.
先讲一下什么是差分约束(有最短路基础):
一、
1、比如一个二元方程组x-y<=w
我们最短路的转移方程是当d[x]>d[y]+w[y->x]成立的时候就会对d[x]重新赋值
而那个二元方程组是当x-y>w的时候才会用到这个方程组来限制
左右移项得到:x>y+w这个条件是不是和最短路的判断条件一样
这就相当与有一条y--->x长为w的边
2、再看一个二元方程组:x-y>=w
我们可以使他的左右两边同时乘与-1,就会转成上一个情况y-x<=-w
这个是当y>x+(-w)的时候才会用到
另外我们还可以按上一个写
这个式子当x-y<w的时候才会用到
左右移项得到:y>x-w
我们发现这两种方法得到的答案一样,所以我们再转移方程的时候可以用左右都乘与-1的方法来使所有方程的符号都一样
3、当出现等号的时候,如果w得知只能取到整数,即x==y
我们可以用两个方程组来代替它
x-y>=0
y-x>=0
然后把他们的符号和题目中其他方程组的符号化为一致就可以了
而、
我们采用这样的方法可以建立一个图,关于这个图的解,有以下三种模式
1、有解,从方程组中可以得到从起点到终点值
2、存在负环,这个样子从起点到终点的距离就无法算出来
3、从起点到终点没有直接或间接连在一起
可见,只有第一种模式才可以算出来结果
可以说本题了。。。。
 
 
题意:有N头牛,编号1-N, 他们进食,按照编号顺序站成一排, 在他们之间有些牛,关系好, 所以希望彼此间的距离不超过一定值,有的关系差,希望距离至少是某值, 此外,牛的性格比较倔,所以有可能由多头牛挤在同一位置上,先给出关系好的信息,(AL, BL, DL),再给出关系差的信息( AH, BH, DH ),求1号和N号的最大距离。
如果不存在任何一种排列方法满足条件,则输出-1,无限大则输出-2;
提取信息:
x:第一头牛
y:第二头牛
第一种:y-x<=w
第二种:y-x>=w
第三:X(i+1)>=X(i)
我们可以把它们的符号都化为一样的
(1)、把他们都转化为<=
1、y-x<=w    <===>   y<=w+x
2、y-x>=w    <===>   x-y<=-w  <===>   x<=y-w
3、X(i+1)>=X(i)   <===>   X(i)<=X(i+1)+0
抽象一下:d[u]<=d[v]+w[v-->u]
当d[u]>d[v]+w[v-->u] 的是侯才会使用这个式子
那就建立一条v------->u值为w的边
那么1、------------>w[x--->y]=w;
       2、------------->w[y--->x]=-w;
       3、------------->w[(i+1)--->i]=0;
就按照这个建图就可以了
代码:

 1 #include<stdio.h>
2 #include<string.h>
3 #include<iostream>
4 #include<algorithm>
5 #include<queue>
6 using namespace std;
7 const int INF=0x3f3f3f3f;
8 int d[1005],dis[1005];
9 struct shudui
10 {
11 int start,value;
12 }str1;
13 vector<shudui>w[1005];
14 queue<int>r;
15 int main()
16 {
17 int n,m,k;
18 scanf("%d%d%d",&n,&m,&k);
19 while(m--)
20 {
21 int x,y,z;
22 scanf("%d%d%d",&x,&y,&z);
23 str1.start=y;
24 str1.value=z;
25 w[x].push_back(str1);
26 }
27 while(k--)
28 {
29 int x,y,z;
30 scanf("%d%d%d",&x,&y,&z);
31 str1.start=x;
32 str1.value=-z;
33 w[y].push_back(str1);
34 }
35 for(int i=1;i<n;++i)
36 {
37 str1.start=i;
38 str1.value=0;
39 w[i+1].push_back(str1);
40 }
41 memset(d,INF,sizeof(d));
42 r.push(1);
43 r.push(1);
44 d[1]=0;
45 int flag=0;
46 while(!r.empty())
47 {
48 int x=r.front();
49 r.pop();
50 int y=r.front();
51 r.pop();
52 dis[x]=0;
53 if(y>n)
54 {
55 flag=1;
56 break;
57 }
58 int len=w[x].size();
59 for(int i=0;i<len;++i)
60 {
61 str1=w[x][i];
62 if(d[str1.start]>d[x]+str1.value)
63 {
64 d[str1.start]=d[x]+str1.value;
65 if(dis[str1.start]) continue;
66 r.push(str1.start);
67 r.push(y+1);
68 }
69 }
70 }
71 if(d[n]!=INF && !flag)
72 printf("%d\n",d[n]);
73 else if(d[n]==INF) printf("-2\n");
74 else if(flag) printf("-1\n");
75 return 0;
76 }

我们还要考虑个问题

本题是让我们求最大值,但是我们建图后为什么求最短路而不是求最长路?

参考博客:https://www.cnblogs.com/cenariusxz/p/4785284.html

首先,我们在最短路初始化的时候所有点的dis值均为INF(极大值),也就是说一开始我们认为n点距离1点是无穷大的。

而对于某一条边 d[v] ≤ d[u] + w(即使≥的也是从这个式子转化而成的),也就是u和v之间距离不能超过w这个条件,只会在d[v] > d[u] + w的时候即u和v之间距离超过w时才会被用来松

弛,而松弛的结果也是使两点距离变成可接受范围内的最大值w,也就是每个边只是松弛到可接受范围内的最大值的。

如果u和v之间的距离已经被其他约束条件限制而小于w了,那么这个条件也就不会起作用也不会使其值更小了,因此最短路求的就是可接受范围内1到n距离的最大值了。

如果是 d[u] - d[v] < w,一般利用整数的情况,就是 d[u] - d[v] ≤ w - 1,也可以顺利建图。

如果是 d[u] - d[v] > w,则建立 d[v] - d[u] ≤ -w-1。

建图之后最大值求最短路,最小值求最长路即可。

更多差分约束题目见:https://blog.csdn.net/whereisherofrom/article/details/78922648

 
 
 
 

S - Layout (最短路&&差分约束)的更多相关文章

  1. POJ-3169 Layout 最短路 差分约束

    题目链接:https://cn.vjudge.net/problem/POJ-3169 题意 Farmer John手下的一些牛有自己喜欢的牛,和讨厌的牛 喜欢的牛之间希望距离在给定距离D之内 讨厌的 ...

  2. [Usaco2005 dec]Layout 排队布局 差分约束

    填坑- 差分约束一般是搞一个不等式组,求xn-x1的最大最小值什么的,求最大值就转化成xa<=xb+w这样的,然后建图跑最短路(这才是最终约束的),举个例子 x1<=x0+2x2<= ...

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

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

  4. POJ 3169 Layout (图论-差分约束)

    Layout Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6574   Accepted: 3177 Descriptio ...

  5. 【最短路·差分约束】洛谷P1250

    题目描述 一条街的一边有几座房子.因为环保原因居民想要在路边种些树.路边的地区被分割成块,并被编号成1..N.每个部分为一个单位尺寸大小并最多可种一棵树.每个居民想在门前种些树并指定了三个号码B,E, ...

  6. Candies POJ - 3159 (最短路+差分约束)

    During the kindergarten days, flymouse was the monitor of his class. Occasionally the head-teacher b ...

  7. bzoj 1731: [Usaco2005 dec]Layout 排队布局 ——差分约束

    Description 当排队等候喂食时,奶牛喜欢和它们的朋友站得靠近些.FJ有N(2<=N<=1000)头奶牛,编号从1到N,沿一条直线站着等候喂食.奶牛排在队伍中的顺序和它们的编号是相 ...

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

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

  9. 最短路 & 差分约束 总结

     一.引例      1.一类不等式组的解 二.最短路       1.Dijkstra       2.图的存储       3.链式前向星       4.Dijkstra + 优先队列      ...

随机推荐

  1. 【JavaWeb】Tomcat 使用

    Tomcat 使用 基础概念 JavaWeb: JavaWeb:所有通过 Java 语言编写可以通过浏览器访问的程序的总称,它是是基于请求和响应来开发的: 请求:客户端给服务器发送数据,即 Reque ...

  2. MySQL45讲笔记-事务隔离级别,为什么你改了数据我看不见

    简单来说,事务就是要保证一组数据库操作,要么全部成功,要么全部失败.在MySQL中,事务至此是在引擎层实现的,但并不是所有的MySQL引擎都支持事务,这也是MyISAM被InnoDB取代的原因之一. ...

  3. 【IMP】IMP导入表的时候,如果表存在怎么办

    在imp导入的时候,如果表存在的话,会追加数据在表中, 所以如果不想追加在表中的话,需要将想导入的表truncate掉后,在imp SQL: truncate table TEST1; imp tes ...

  4. js千分位分隔,数字货币化方法学习记录

    js千分位分隔,数字货币化-4种方法(含正则) 方法1-整数货币化 // 整数货币化 function intCurrency(num) { var reg = new RegExp("^[ ...

  5. JS获取本机地址,生成地图

    dome代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...

  6. mybatis源码分析之走进缓存

    之前写了一篇关于mybatis缓存的读后感,想了想还是把缓存模块简单分析一下,附赠下载地址:https://github.com/MyBatis/MyBatis-3,github直接搜排名很靠前的. ...

  7. Redis 实战 —— 08. 实现自动补全、分布式锁和计数信号量

    自动补全 P109 自动补全在日常业务中随处可见,应该算一种最常见最通用的功能.实际业务场景肯定要包括包含子串的情况,其实这在一定程度上转换成了搜索功能,即包含某个子串的串,且优先展示前缀匹配的串.如 ...

  8. 前端中的script标签

    script标签中的重要属性! . 浏览器解析行内脚本的方式决定了它在看到字符串时,会将其当成结束的 标签.想避免这个问题,只需要转义字符"\" ①即可: 要包含外部文件中的 Ja ...

  9. ubuntu更新下载软件卡住0% [Connecting to archive.ubuntu.com (2001:67c:1360:8001::23)]

    一台ubuntu系统,查看硬件和配置环境的时候发现下载卡住了 根据提示就是有ipv6地址,系统也是配置了ipv6地址的.海外机器,而且可以ping通域名 最佳解决方案 我想出了如何让apt-get再次 ...

  10. Windows Server 2012 R2 英文版汉化安装中文语言包教程更改为中文版

    是这样的,一台海外的windows机器默认是英文版的,但是特别费劲用起来,就更改为中文版,因为海外的供应商并不提供中文版镜像. 1.首先打开控制面板,找到add language,拉到底就是有中文,很 ...