题干:

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.

解题报告:

差分约束系统+spfa。这时候就不能用Dijkstra啦,因为有负权了。当成个模板记住也好啊。

AC代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int INF = 0x3f3f3f3f;
int head[10000 + 5];
bool vis[10000 + 5];
int ci[10000 + 5],dis[10000 + 5];
int cnt,n;
struct Edge{int to;int w;int ne;
}e[20000 + 5];
void add(int u,int v,int w) {e[cnt].to = v;e[cnt].w = w;e[cnt].ne = head[u];head[u] = cnt++;
}
int spfa(int st) {dis[st] = 0;vis[st] = 1;queue<int > q;q.push(st);while(!q.empty()) {int cur = q.front();q.pop();vis[cur] = 0;for(int i = head[cur]; i!=-1; i = e[i].ne) {if(dis[e[i].to]>dis[cur]+e[i].w ) {dis[e[i].to]=dis[cur]+e[i].w ;if(vis[e[i].to] == 0) {vis[e[i].to] = 1;q.push(e[i].to);ci[e[i].to]++;if(ci[e[i].to]>=n) return -1;} }}}if(dis[n] == INF) return -2;else return dis[n];
}
//void init() {
//  cnt = 0;
//  memset()
//}
int main()
{int a,b,w;int ml,md;while(~scanf("%d%d%d",&n,&ml,&md)) {cnt = 0;memset(head, -1, sizeof(head));memset(ci,0,sizeof(ci));memset(vis,0,sizeof(vis));memset(dis,INF,sizeof(dis));while(ml--) {scanf("%d%d%d",&a,&b,&w);if(a>b)swap(a,b);add(a,b,w);}while(md--) {scanf("%d%d%d",&a,&b,&w);if(a<b) swap(a,b);add(a,b,-w);}printf("%d\n",spfa(1));}return 0 ;
}

【POJ - 3169】 Layout(差分约束+spfa)(当板子记?)相关推荐

  1. O - Layout POJ - 3169(差分约束)

    O - Layout POJ - 3169 参考 思路: 限制条件 : 最大距离不超过w d[v] - d[u] <= w; 最小距离超过w d[v] - d[u] >= w; 移项得 d ...

  2. HDU3440(差分约束+SPFA算法)

    题意:两栋房子之间的最大距离为D,也就是A-B<=D,现在求出最矮和最高房子之间的最大距离 思路:差分约束+SPFA算法: 当问题可以转化为形如一组 xi‑x'i<=yi 或一组 xi‑x ...

  3. poj1201(差分约束+SPFA)

    看到这道题,其实就是和poj1716是差不多的 题意:给出n个闭整数区间[ai,bi]和n个整数C1,.,cn.计算具有区间[ai,bi]的至少ci公共元素的整数集Z的最小大小,对于每一个i=1,2, ...

  4. poj3169(差分约束+SPFA)

    题意:FJ有N头牛,这些牛都站在一条直线上等待,但是现在给出了一些条件: 1.首先列出哪些牛之间彼此喜欢,以及之间的最大距离,也就是A-B<=X 2.随后列出哪些牛之间彼此不喜欢,以及之间的最小 ...

  5. [poj 1364]King[差分约束详解(续篇)][超级源点][SPFA][Bellman-Ford]

    题意 有n个数的序列, 下标为[1.. N ], 限制条件为: 下标从 si 到 si+ni 的项求和 < 或 > ki. 一共有m个限制条件. 问是否存在满足条件的序列. 思路 转化为差 ...

  6. BZOJ.4500.矩阵(差分约束 SPFA判负环 / 带权并查集)

    BZOJ 差分约束: 我是谁,差分约束是啥,这是哪 太真实了= = 插个广告:这里有差分约束详(并不)解. 记\(r_i\)为第\(i\)行整体加了多少的权值,\(c_i\)为第\(i\)列整体加了多 ...

  7. poj1716(差分约束+SPFA)

    题意:整数间隔[a,b],a<b,是以a开头和以b结尾的所有连续整数的集合.在包含至少两个不同整数的集合中找到每个间隔的最小元素数. 思路:采用差分约束算法:当问题可以转化为形如一组 xi‑x' ...

  8. HDU 1384 Intervals【差分约束-SPFA】

    类型:给出一些形如a−b<=k的不等式(或a−b>=k或a−b<k或a−b>k等),问是否有解[是否有负环]或求差的极值[最短/长路径]. 例子:b−a<=k1,c−b& ...

  9. P1993-小K的农场【差分约束,SPFA】

    正题 题目链接:https://www.luogu.org/problemnew/show/P1993 题目大意 有若干个条件 Wa+w>WbW_a+w>W_bWa​+w>Wb​ W ...

最新文章

  1. 计算机的几种类型单词、快捷键
  2. mysql8.0 linux安装自启动_Linux系统安装部署MySQL8.0.12特详细教程
  3. php中显示不出图像,php – 无法显示图像,因为它包含错误
  4. IT人的地摊不就是开源么 | 凌云时刻
  5. python整数因式分解
  6. mac移动硬盘安装linux系统安装教程,移动硬盘上安装ubuntu系统
  7. 今年职高计算机数学高考试题,湖南职高对口数学高考试卷
  8. 数据库系统---数据挖掘
  9. python打开记事本并输入内容_打开记事本输入文字
  10. 数据分析模型 第十一章
  11. 《统计之美:人工智能时代的科学思维》(李舰,海恩)读书记录 - 1
  12. 重构于 Vite:我如何做 SSG、静态资源发布以及自动化部署
  13. ERP系统容灾方案析投入产出比例与维护管理成本分析
  14. 微积分入门(SXT版)
  15. postgre的replace函数
  16. OpenSIPS的无状态及有状态路由
  17. 「直播回放」数仓如何落地
  18. 关于Photoshop CC 2019
  19. 英语听说计算机考试演练专用,新中高考英语听说机考时间确定,9月底将进行模拟演练...
  20. 关于东芝BG3 PCMARK性能超高的研究

热门文章

  1. [Leetcode][第141、142题][JAVA][环形链表][哈希表][快慢指针][数学推理]
  2. java 数据库连接池 开源_开源自己开发的一个JAVA数据库连接池,效果还算可以。...
  3. mysql 读取comment_Mysql 获取表的comment 字段
  4. linux网卡IO,浅谈Linux 网络 I/O 模型简介(图文)
  5. 银行招聘网计算机类笔试,中国人民银行计算机类笔试模拟题
  6. echarts 生成 迁徙图_echarts3 迁徙图 迁入迁出(示例代码)
  7. libsvm python_LibSVM for Python 使用
  8. p3d gauge 尺寸问题
  9. WinCE控制面板添加应用程序
  10. WinCE驱动调试助手V2.5