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. u-boot之NAND启动与NOR启动的区别

    nand启动与nor启动的区别主要分为以下几部分说明: 1.nand flash与nor flash的最主要区别 2.s3c2440的nand启动与nor启动原理 3.nand启动与nor启动的时候u ...

  2. SDK Manager 基础下载

    双击SDK Manager打开Android SDK Manager. 全选以下几项 创建新项目 更改gradle的地址: 更改app的build.gradle: android { buildToo ...

  3. (转)android拨打电话崩溃6.0以上实时动态权限申请

    文章转自:http://blog.csdn.net/qq_29988575/article/details/54909213 6.0以下手机正常,6.0以上的却崩溃 解决方法: targetSdkVe ...

  4. Hadoop(一) HADOOP简介

    1. HADOOP背景介绍 1.1 什么是HADOOP HADOOP是apache旗下的一套开源软件平台 HADOOP提供的功能:利用服务器集群,根据用户的自定义业务逻辑,对海量数据进行分布式处理 H ...

  5. [Robot Framework] 调用ExcelLibrary

    安装ExcelLibrary for Robot Framework 参考 : http://navinet.github.io/robotframework-excellibrary/ 打开wind ...

  6. InputMethodManagerService处理输入法——监听APK变动

    android\frameworks\base\services\java\com\android\server\InputMethodManagerService.java public Input ...

  7. 【算法】DP解决旅行路径问题

    问题描述 : After coding so many days,Mr Acmer wants to have a good rest.So travelling is the best choice ...

  8. 【Linux】 Ncures库的介绍与安装

    Ncures库的介绍 ncurses(new curses)是一套编程库,它提供了一系列的函数以便使用者调用它们去生成基于文本的用户界面. ncurses名字中的n意味着“new”,因为它是curse ...

  9. FMS是什么?

  10. Firefox,chrome,IE上传图片预览

    首先判断IE或是Firefox,chrome.本文只测试了IE8中和Firefox,chrome是不一样的. 判断是否IE: if(-[1,]){//判断浏览器不是IE    //alert((-[1 ...