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

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.

 
---------
 
对于差分系统的讲解在这个讲的比较清楚
https://blog.csdn.net/mengxiang000000/article/details/52613328
 
如果仅仅用Bellman-Ford会TLE
所以需要优化,使用SPFA
http://www.cnblogs.com/shadowland/p/5870640.html
这个对spfa讲解的比较清楚
 
 
AC代码:
#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std; struct edge{
int from,to,cost;
};
int visit[];
int cnt[];
int d[];
queue<int> que;
int n,size,sum;
vector<edge> es; int main(){
int ml,md;
cin>>n>>ml>>md;
sum=ml+md;
es.resize(sum);
for(int i=;i<ml;i++){
int a,b,c;
cin>>a>>b>>c;
edge e={a,b,c};
es[i]=e;
}
for(int i=ml;i<ml+md;i++){
int a,b,c;
cin>>a>>b>>c;
edge e={b,a,-c};
es[i]=e;
}
for(int i=;i<n;i++){
edge e={i+,i,};
es.push_back(e);
}
size=es.size(); for(int i=;i<=n;i++)
d[i]=INT_MAX;
d[]=;
que.push();
visit[]=;
cnt[]++;
while(!que.empty()){
int p=que.front();
que.pop();
visit[p]=;
for(int i=;i<size;i++){
edge e=es[i];
if(e.from==p&&d[e.from]+e.cost<d[e.to]){
d[e.to]=d[e.from]+e.cost;
if(visit[e.to]==){
cnt[e.to]++;
if(cnt[e.to]>=n){
cout<<-;
return ;
}
que.push(e.to);
visit[e.to]=;
}
}
}
}
if(d[n]==INT_MAX)
cout<<-;
else
cout<<d[n];
return ;
}

POJ3169--Layout(SPFA+差分系统)的更多相关文章

  1. POJ3169:Layout(差分约束)

    Layout Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15705   Accepted: 7551 题目链接:http ...

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

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

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

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

  4. POJ-3169 Layout (差分约束+SPFA)

    POJ-3169 Layout:http://poj.org/problem?id=3169 参考:https://blog.csdn.net/islittlehappy/article/detail ...

  5. POJ3169 Layout(差分约束系统)

    POJ3169 Layout 题意: n头牛编号为1到n,按照编号的顺序排成一列,每两头牛的之间的距离 >= 0.这些牛的距离存在着一些约束关系:1.有ml组(u, v, w)的约束关系,表示牛 ...

  6. O - Layout(差分约束 + spfa)

    O - Layout(差分约束 + spfa) Like everyone else, cows like to stand close to their friends when queuing f ...

  7. Did Pong Lie? (差分系统 判负环)

    Did Pong Lie? 时间限制: 5 Sec  内存限制: 128 MB提交: 68  解决: 15[提交][状态][讨论版] 题目描述 Doctor Pong has two arrays o ...

  8. (简单) POJ 3169 Layout,差分约束+SPFA。

    Description Like everyone else, cows like to stand close to their friends when queuing for feed. FJ ...

  9. POJ 3169 Layout 【差分约束】+【spfa】

    <题目链接> 题目大意: 一些母牛按序号排成一条直线.有两种要求,A和B距离不得超过X,还有一种是C和D距离不得少于Y,问可能的最大距离.如果没有最大距离输出-1,如果1.n之间距离任意就 ...

随机推荐

  1. 点评10款Github上最火爆的国产开源项目

    衡量一个开源产品好不好,看看产品在Github的Star数量就知道了.由此可见,Github已经沦落为开源产品的“大众点评”了. 一个开源产品希望快速的被开发者知道.快速的获取反馈,放到Github上 ...

  2. Dottrace 10.0.2 使用心得

    开发环境vs2015 软件:JetBrains dotTrace 10.0.2 刚开始不知道怎么下手,多看了一会还有一位仁兄的解释.算是对某个功能小有入门了. 当前会查看某个方法在抓取快照时间它的执行 ...

  3. linux 管道符与通配符

    ###管道符 *命令格: 命令1 | 命令2 //命令1的正确输出作为命令2的操作对象 ll | more netstat -an | grep xxx 通配符 类似于正则表达式 ? 一个以上 [] ...

  4. android使用Pull解析来自服务器的xml文件时出现错误以及解决方案

    直接上代码,代码中有详细注释: 1 public class CheckUpdateManager { 2 private static final String TAG = "CheckU ...

  5. Pandas选择数据

    1.简单筛选 >>> dates = pd.date_range(', periods=6) >>> df = pd.DataFrame(np.arange(24) ...

  6. BPF+XDP比较全的资料都在这里

    Dive into BPF: a list of reading material Sep 1, 2016 • Quentin Monnet◀Table of contents What is BPF ...

  7. (O)jquery:e.target和this的区别(如何使事件委托后,被选元素的子元素不被选中)

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  8. tableView上出现空白的解决办法

    创建tableView后,出现如下效果       解决办法: self.automaticallyAdjustsScrollViewInsets = NO; 个人认为,应该是取消系统默认行为,保证界 ...

  9. POJ 3169.Layout 最短路

    Layout Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11612   Accepted: 5550 Descripti ...

  10. JSTL(JSP标准标签库)

    JSP标准标签库(JavaServer Pages Tag Library, JSTL)是一个定制JSP标签库的集合,封装了JSP应用的通用核心功能.用来解决像遍历Map或集合.条件测试.XML处理, ...