一、题目

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.

二、思路

二、思路&心得

  • POJ-3169:查分约束系统,利用约束条件,将问题转化为最短路径问题,并利用Bellman-Ford或SPFA算法求解
  • 本题需要考虑边的方向关系,虽然感觉是无向图,但是最后却还是初始化成有向图

三、代码

#include<cstdio>
#include<climits>
#include<algorithm>
#define MAX_N 10005
#define MAX_M 30005
#define MAX_D 2000005
using namespace std; int N, ML, MD; int A, B, D; int dist[MAX_N]; struct Edge {
int from;
int to;
int cost;
} E[MAX_M]; int Bellman_Ford(int s) {
for (int i = 1; i <= N; i ++) {
dist[i] = MAX_D;
}
dist[s] = 0;
int edgeNum = ML + MD + N - 1;
for (int i = 0; i < N; i ++) {
for (int j = 0; j < edgeNum; j ++) {
if (dist[E[j].from] + E[j].cost < dist[E[j].to]) {
if (i == N - 1) return -1;
dist[E[j].to] = dist[E[j].from] + E[j].cost;
}
}
}
return dist[N] == MAX_D ? -2 : dist[N];
} void solve() {
/**
* 图的初始化
*/
for (int i = 0; i < ML; i ++) {
scanf("%d %d %d", &A, &B, &D);
if (A > B) swap(A, B);
E[i].from = A, E[i].to = B, E[i].cost = D;
}
for (int i = 0; i < MD; i ++) {
scanf("%d %d %d", &A, &B, &D);
if (A > B) swap(A, B);
E[ML + i].from = B, E[ML + i].to = A, E[ML + i].cost = -D;
}
for (int i = 0; i < N - 1; i ++) {
E[ML + MD + i].from = i + 2, E[ML + MD + i].to = i + 1, E[ML + MD + i].cost = 0;
}
printf("%d\n", Bellman_Ford(1));
} int main() {
while (~scanf("%d %d %d", &N, &ML, &MD)) {
solve();
}
return 0;
} x ​

【图论】POJ-3169 差分约束系统的更多相关文章

  1. POJ - 3169 差分约束

    题意:n头牛,按照编号从左到右排列,两头牛可能在一起,接着有一些关系表示第a头牛与第b头牛相隔最多与最少的距离,最后求出第一头牛与最后一头牛的最大距离是多少,如         果最大距离无限大则输出 ...

  2. Intervals poj 1201 差分约束系统

    Intervals Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 22503   Accepted: 8506 Descri ...

  3. POJ 3159 Candies (图论,差分约束系统,最短路)

    POJ 3159 Candies (图论,差分约束系统,最短路) Description During the kindergarten days, flymouse was the monitor ...

  4. POJ 3169 Layout (差分约束系统)

    Layout 题目链接: Rhttp://acm.hust.edu.cn/vjudge/contest/122685#problem/S Description Like everyone else, ...

  5. POJ 3169 Layout 差分约束系统

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

  6. 差分约束系统 + spfa(A - Layout POJ - 3169)

    题目链接:https://cn.vjudge.net/contest/276233#problem/A 差分约束系统,假设当前有三个不等式 x- y <=t1 y-z<=t2 x-z< ...

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

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

  8. 【POJ 1716】Integer Intervals(差分约束系统)

    id=1716">[POJ 1716]Integer Intervals(差分约束系统) Integer Intervals Time Limit: 1000MS   Memory L ...

  9. 【POJ 1275】 Cashier Employment(差分约束系统的建立和求解)

    [POJ 1275] Cashier Employment(差分约束系统的建立和求解) Cashier Employment Time Limit: 1000MS   Memory Limit: 10 ...

随机推荐

  1. ubuntu16.04编译安装opencv3.4.6

    1.下载压缩包:https://github.com/opencv/opencv,在Branch栏选择3.4版本,clone下载 2.安装cmake及依赖库,打开终端,输入以下命令: sudo apt ...

  2. 不安分的android开发者(小程序初尝试,前后台都自己做)

    前言 作为一个稍微有点想法的程序员来说,拥有一个自己开发,自己运营,完全属于自己的应用,应该是很多人的梦想.刚毕业那会,自己的工作是做游戏,于是也和朋友业余时间开发一些小游戏玩玩,可是终究不成气候,而 ...

  3. Firebird3基本使用

    解决C#无法访问的情况:1. 使用FirebirdSql.Data.FirebirdClient 5版本以上.2.修改Firebird.conf配置文件WireCrypt为Enabled#WireCr ...

  4. 【LG3240】[HNOI2015]实验比较

    题面 洛谷 题解 30pts 爆搜即可. 100pts 题意描述里有一句:"对每张图片\(i\),小\(D\)都最多只记住了某一张质量不比\(i\)差的另一张图片\(K_i\)." ...

  5. Oracle GUID转换为String

    Oracle中guid属于Raw(16)类型, 查询的时候如果不使用下面的函数, 程序中得到的是数组(byte[]). 在extjs环境下, 会带来数组的反序列化问题(newtonsoft.json) ...

  6. [JOISC2018]道路建设 LCT

    [JOISC2018]道路建设 LOJ传送门 考的时候打的大暴力,其实想到了LCT,但是思路有点没转过来.就算想到了估计也不能切,我没有在考场写LCT的自信... 其实这题不是让你直接用LCT维护答案 ...

  7. Dbzoj#3188. [Coci 2011]Upit

    写道数据结构练练手哈哈哈 // It is made by XZZ #include<cstdio> #include<algorithm> #include<cstdl ...

  8. node.js学习笔记(二)——回调函数

    Node.js 异步编程的直接体现就是回调. 那什么是回调呢?回调指的是将一个函数作为参数传递给另一个函数,并且通常在第一个函数完成后被调用.需要指明的是,回调函数不是由该函数的实现方直接调用,而是在 ...

  9. python 利用urllib 获取办公区公网Ip

    import json,reimport urllib.requestdef GetLocalIP(): IPInfo = urllib.request.urlopen("http://ip ...

  10. c++的重载 缺省参数和命名空间详解

    参加了几次笔试,发现有很多c++方面的问题被卡了.从现在开始进攻c++.之后会陆续更新c++学习笔记. 先说说我学习的书籍,大家如果有好的书籍推荐,感谢留言. 暂时是在看这些书自学. 1.C++介绍. ...