题目链接:http://poj.org/problem?

id=3169

Description

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

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

Sample Output

  1. 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.

Source

题意:

给出N头牛,他们是依照顺序编号站在一条直线上的,同意有多头牛在同一个位置!

给出ML对牛,他们同意之间的距离小于等于W

给出MD对牛,他们之间的距离必须是大于等于W的

给出ML+MD的约束条件,求1号牛到N号的最大距离dis[N]。

假设dis[N] = INF,则输出-2。

假设他们之间不存在满足要求的方案,输出-1

其余输出dis[N];

PS:http://blog.csdn.net/zhang20072844/article/details/7788672

代码例如以下:

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <stack>
  4. #include <iostream>
  5. #include <algorithm>
  6. using namespace std;
  7. #define INF 0x3f3f3f3f
  8. #define N 20000
  9. #define M 20000
  10. int n, m, k;
  11. int Edgehead[N], dis[N];
  12. struct Edge
  13. {
  14. int v,w,next;
  15. } Edge[2*M];
  16. bool vis[N];
  17. int cont[N];
  18. void Addedge(int u, int v, int w)
  19. {
  20. Edge[k].next = Edgehead[u];
  21. Edge[k].w = w;
  22. Edge[k].v = v;
  23. Edgehead[u] = k++;
  24. }
  25. int SPFA( int start)//stack
  26. {
  27. int sta[N];
  28. memset(cont,0,sizeof(cont);
  29. int top = 0;
  30. for(int i = 1 ; i <= n ; i++ )
  31. dis[i] = INF;
  32. dis[start] = 0;
  33. ++cont[start];
  34. memset(vis,false,sizeof(vis));
  35. sta[++top] = start;
  36. vis[start] = true;
  37. while(top)
  38. {
  39. int u = sta[top--];
  40. vis[u] = false;
  41. for(int i = Edgehead[u]; i != -1; i = Edge[i].next)//注意
  42. {
  43. int v = Edge[i].v;
  44. int w = Edge[i].w;
  45. if(dis[v] > dis[u] + w)
  46. {
  47. dis[v] = dis[u]+w;
  48. if( !vis[v] )//防止出现环
  49. {
  50. sta[++top] = v;
  51. vis[v] = true;
  52. }
  53. if(++cont[v] > n)//有负环
  54. return -1;
  55. }
  56. }
  57. }
  58. return dis[n];
  59. }
  60. int main()
  61. {
  62. int u, v, w;
  63. int c;
  64. int ml, md;
  65. while(~scanf("%d%d%d",&n,&ml,&md))//n为目的地
  66. {
  67. k = 1;
  68. memset(Edgehead,-1,sizeof(Edgehead));
  69. for(int i = 1 ; i <= ml; i++ )
  70. {
  71. scanf("%d%d%d",&u,&v,&w);
  72. Addedge(u,v,w);
  73. }
  74. for(int i = 1 ; i <= md; i++ )
  75. {
  76. scanf("%d%d%d",&u,&v,&w);
  77. Addedge(v,u,-w);
  78. }
  79. for(int i = 1; i < n; i++)
  80. {
  81. Addedge(i+1,i,0);
  82. }
  83. int ans = SPFA(1);//从点1開始寻找最短路
  84. if(ans == INF)
  85. {
  86. printf("-2\n");
  87. }
  88. else
  89. {
  90. printf("%d\n",ans);
  91. }
  92. }
  93. return 0;
  94. }

POJ 3169 Layout(差分约束啊)的更多相关文章

  1. POJ 3169 Layout(差分约束+链式前向星+SPFA)

    描述 Like everyone else, cows like to stand close to their friends when queuing for feed. FJ has N (2 ...

  2. POJ 3169 Layout (差分约束)

    题意:给定一些母牛,要求一个排列,有的母牛距离不能超过w,有的距离不能小于w,问你第一个和第n个最远距离是多少. 析:以前只是听说过个算法,从来没用过,差分约束. 对于第 i 个母牛和第 i+1 个, ...

  3. poj 3169 Layout 差分约束模板题

    Layout Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6415   Accepted: 3098 Descriptio ...

  4. POJ 3169 Layout(差分约束 线性差分约束)

    题意: 有N头牛, 有以下关系: (1)A牛与B牛相距不能大于k (2)A牛与B牛相距不能小于k (3)第i+1头牛必须在第i头牛前面 给出若干对关系(1),(2) 求出第N头牛与第一头牛的最长可能距 ...

  5. ShortestPath:Layout(POJ 3169)(差分约束的应用)

                布局 题目大意:有N头牛,编号1-N,按编号排成一排准备吃东西,有些牛的关系比较好,所以希望他们不超过一定的距离,也有一些牛的关系很不好,所以希望彼此之间要满足某个关系,牛可以 ...

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

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

  7. Bellman-Ford算法:POJ No.3169 Layout 差分约束

    #define _CRT_SECURE_NO_WARNINGS /* 4 2 1 1 3 10 2 4 20 2 3 3 */ #include <iostream> #include & ...

  8. POJ 3169 Layout 差分约束系统

    介绍下差分约束系统:就是多个2未知数不等式形如(a-b<=k)的形式 问你有没有解,或者求两个未知数的最大差或者最小差 转化为最短路(或最长路) 1:求最小差的时候,不等式转化为b-a>= ...

  9. POJ 3169 Layout (spfa+差分约束)

    题目链接:http://poj.org/problem?id=3169 差分约束的解释:http://www.cnblogs.com/void/archive/2011/08/26/2153928.h ...

随机推荐

  1. 使用Redis的理由

    Redis是一个远程内存数据库,它不仅性能强劲,而且还具有复制特性以及为解决问题而生的独一无二的数据模型.Redis提供了5种不同类型的数据结构,各式各样的问题都可以很自然地映射到这些数据结构上:Re ...

  2. bzoj 1025 [SCOI2009]游戏(置换群,DP)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1025 [题意] 给定n,问1..n在不同的置换下变回原序列需要的不同排数有多少种. [ ...

  3. CSS基础(背景、文本、列表、表格、轮廓)

    CSS 背景属性 属性 描述 background 简写属性,作用是将背景属性设置在一个声明中. background-attachment 背景图像是否固定或者随着页面的其余部分滚动. backgr ...

  4. 虚拟化技术对比:Xen vs KVM

    恒天云:http://www.hengtianyun.com/download-show-id-68.html 一.说明 本文主要从功能方面和性能方面对Xen和KVM对比分析,分析出其优缺点指导我们恒 ...

  5. 树莓派I2C连接18B20

    按图连接设备 载入模块 sudo modprobe w1-gpio sudo modprobe w1-therm cd /sys/bus/w1/devices/ 显示结果 ls pi@raspberr ...

  6. mount失败

    又一次遇到mount失败,提示文件系统类型错误.选项错误.有坏超级块等.之前是在ubuntu 14.04 lts desktop上挂载windows下共享文件夹遇到的.这次具体的环境如下:CentOS ...

  7. 第二百八十四天 how can I 坚持

    又是一个周一.今天感觉过得好艰辛啊,幸好晚上程秀通过生日请客,吃了顿大餐,还拿回了一瓶酒.哈哈. 其他也没什么了.晚上玩的挺好.不过,回来,老是渴,一直想喝水,现在是又困,又累啊,睡觉了.

  8. Android实例-读取设备联系人(XE8+小米2)

    相关资料: http://www.colabug.com/thread-1071065-1-1.html 结果: 1.将权限打开Read contacts设置为True,不然报图一的错误. 2.搜索空 ...

  9. jstat用法

    jstat(JVM Statistics Monitoring Tool)是用于监视虚拟机各种运行状态信息的命令行工具.它可以显示本地或者远程虚拟机进程中的类装载.内存.垃圾收集.JIT编译等运行数据 ...

  10. thymeleaf中的内联[ [ ] ]

    一.文本内联 [[…]]之间的表达式在Thymeleaf被认为是内联表达式,在其中您可以使用任何类型的表达式,也会有效th:text属性. <p>Hello, [[${session.us ...