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. c# sharpsvn 客户端开发测试

    1:没有工作副本,上传时会报错的. 会提示本地目录不是not a working copy   而此中文名称就是工作副本, 本地要与svn服务器公用的一个文件夹. 所以每次要checkout 然后才能 ...

  2. Mac下安装社区版MongoDB

    MongoDB下载地址:https://www.mongodb.com/download-center?_ga=2.98072543.1777419256.1515472368-391344272.1 ...

  3. LINUX查看网卡UUID

    有时我们不小心将/etc/sysconfig/network-scripts/ifcfg-eth0(可以通过此文件进行查看UUID)删除或者损坏,要重新编辑ifcfg-eth0文件时不知道网卡的UUI ...

  4. 简单的node 服务端 响应get,返回json数据;

    原文:http://blog.csdn.net/xn_28/article/details/50837019 const http = require('http'); const hostname ...

  5. swift 中使用weak self

    request.responseString(encoding: NSUTF8StringEncoding) {[weak self] (res) -> Void in if let stron ...

  6. [linux]Linux如何查看文件中的中间部分内容

    最基本的是cat.more和less. 1. 如果你只想看文件的前5行,可以使用head命令,如: head -5 /etc/passwd 2. 如果你想查看文件的后10行,可以使用tail命令,如: ...

  7. 微信小程序获取当前位置

    详细参数说明请看小程序api文档:https://developers.weixin.qq.com/miniprogram/dev/api/wx.openLocation.html wx.getLoc ...

  8. 在Tomcat中部署Spring jpetstore

    第三篇:在Tomcat中部署Spring jpetstore 博客分类: Java之web SpringTomcatMySQLJDBCMVC  Spring samples中的jpetstore,基于 ...

  9. Java第14章笔记

    Java 中无参无返回值和带参带返回值习题 编写一个 Java 程序,实现输出学生年龄的最大值 要求: 1. 要求通过定义无参带返回值的方法来实现,返回值为最大年龄 2. 方法中将​学生年龄保存在数组 ...

  10. unity技巧

    在之前的程序编写过程中,虽然对相关的方法进行了实例化,但是在运行的时候总是会出现“未将对象引用设置到对象的实例”,出现该种问题的原因是由于在实例化后,没有对实例化进行引用赋值,所以导致相关变量无法在其 ...