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
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.

Source

正解:差分约束系统+SPFA

解题报告:

大概题意是给定排成一列的牛,然后两头牛之间的距离可能要大于等于某个值或者小于等于某个值,问是否存在或者终点是否可以无限远。

  以前在codevs上面做过一道差分约束系统的题,然后就学会了这种神奇的思想。其实思想很简单,结合图论的话还是很有用的。

  考虑题意,需要求1到n的最大距离。题目中给了很多限制条件,比如说x2-x1<=3,x4-x2>=6这样的条件。我们考虑像x2-x1<=3这样的条件,因为我们想让距离尽可能大,就要使距离最大化,然后建图,1向2连一条权值为3的边。那么像x4-x2>=6这样大于的怎么办呢,我们就可以把它变成x2-x4<=-6,边权为负即可。然后图上跑SPFA。

  接着是个很重要的问题,是最短路还是最长路呢?按理说要想距离大应该跑最长路,但是我们想,我们这个图是怎么建的,根据每个条件的最大条件连边,那么说明我们肯定要取所有对这个点的约束中最小的那个(取交),所以只会越来越小。不难想到,最后求出的dis[n]就是我们要求的。

题意中的-1、-2怎么特判呢?如果有负权环就说明不可能,记一下每个点入队n次就说明有负权环。而可以无限大则说明还到不了n,则说明dis[n]仍为初值

轻松AC,代码如下:

 //It is made by jump~
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <ctime>
#include <vector>
#include <queue>
#include <map>
#ifdef WIN32
#define OT "%I64d"
#else
#define OT "%lld"
#endif
using namespace std;
typedef long long LL;
const int MAXN = ;
const int MAXM = ;
const int inf = (<<);
int n,m1,m2;
int first[MAXN],to[MAXM],next[MAXM],w[MAXM],ecnt;
int dis[MAXN];
queue<int>Q;
bool pd[MAXN];
int cnt[MAXN]; inline int getint()
{
int w=,q=;
char c=getchar();
while((c<'' || c>'') && c!='-') c=getchar();
if (c=='-') q=, c=getchar();
while (c>='' && c<='') w=w*+c-'', c=getchar();
return q ? -w : w;
} inline void link(int x,int y,int z){
next[++ecnt]=first[x]; first[x]=ecnt; to[ecnt]=y; w[ecnt]=z;
} inline bool spfa(){
Q.push(); pd[]=;
for(int i=;i<=n;i++) dis[i]=inf;
while(!Q.empty()){
int u=Q.front(); Q.pop(); pd[u]=;
for(int i=first[u];i;i=next[i]) {
int v=to[i];
if(dis[v]>dis[u]+w[i]) {
dis[v]=dis[u]+w[i];
if(!pd[v]) {
Q.push(v);
pd[v]=;
cnt[v]++;
if(cnt[v]>=n) return false;
}
}
}
}
if(dis[n]==inf) printf("-2");
else printf("%d",dis[n]);
return true;
} inline void solve(){
n=getint(); m1=getint(); m2=getint();
int x,y,z;
for(int i=;i<=m1;i++) {
x=getint();y=getint();z=getint();
link(x,y,z);
}
for(int i=;i<=m2;i++) {
x=getint(); y=getint(); z=getint();
link(y,x,-z);
}
if(!spfa()) printf("-1");
} int main()
{
solve();
return ;
}

POJ3169 Layout的更多相关文章

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

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

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

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

  3. POJ3169:Layout(差分约束)

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

  4. POJ-3169 Layout 最短路 差分约束

    题目链接:https://cn.vjudge.net/problem/POJ-3169 题意 Farmer John手下的一些牛有自己喜欢的牛,和讨厌的牛 喜欢的牛之间希望距离在给定距离D之内 讨厌的 ...

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

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

  6. POJ3169:Layout(差分约束)

    http://poj.org/problem?id=3169 题意: 一堆牛在一条直线上按编号站队,在同一位置可以有多头牛并列站在一起,但编号小的牛所占的位置不能超过编号大的牛所占的位置,这里用d[i ...

  7. 转自作者:phylips@bmy

    差分约束系统 2008-11-28 20:53:25|  分类: 算法与acm|举报|字号 订阅     出处:http://duanple.blog.163.com/blog/static/7097 ...

  8. 【POJ3169 】Layout (认真的做差分约束)

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

  9. 【图论】POJ-3169 差分约束系统

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

随机推荐

  1. java 16-6 泛型

    ArrayList存储字符串并遍历 我们按照正常的写法来写这个程序, 结果确出错了. 为什么呢? 因为我们开始存储的时候,存储了String和Integer两种类型的数据. 而在遍历的时候,我们把它们 ...

  2. C#带cookie Post和Get方式发送数据,保持cookie

    在实际编程中,可能需要读取特定网页的信息,但很多网站需要用户登录后,才能够获取相关的页面内容,这就需要编程者先临时存储当前的cookie,在C#中可以使用CookieContainer 对象来保存登录 ...

  3. first root

    题目给出一棵二叉树的中序和后序排列.求出它的先序排列[提示]通过对比二叉树的中序和后序排列,我们可以找出根节点及左右子树.同样的,也可以通过对比左子树的中序和后序排列,找出左子树的根节点…….可见,该 ...

  4. TIF、JPG图片手动添加地理坐标的方法(转载)

    题目:为TIF.JPG图片添加地理坐标/平面直角坐标. 图片来源:GOOGLE EARTH.(当然也可以是其他知道四角点坐标的图片) 截图工具:GEtscreen(此软件截图时可以自动生成图片四角点坐 ...

  5. Python-操作Memcache、Redis、RabbitMQ、

    Memcache 简述: Memcache是一套分布式的高速缓存系统,由LiveJournal的Brad Fitzpatrick开发,但目前被许多网站使用以提升网站的访问速度,尤其对于一些大型的.需要 ...

  6. 泛型委托 Predicate/Func/Action

    Predicate 泛型委托  表示定义一组条件并确定指定对象是否符合这些条件的方法.此委托由 Array 和 List 类的几种方法使用,用于在集合中搜索元素.看看下面它的定义: // Summar ...

  7. Silverlight自定义控件开发:温度计

    由于在实际项目中需要实时显示采集到的空气温湿度,土壤温湿度值,需要用比较显眼并且清楚明了的方式来展示,这里我们准备采用温度计的方式来进行.一方面是因为大家都熟悉这个,知道怎么去看:同时,温度计本身也比 ...

  8. CSS 实现加载动画之七-彩环旋转

    今天整理的这个动画估计大家都不会陌生,彩环旋转,看过之后是不是觉得很熟悉,对,这个就是优酷视频APP里面的加载动画.本人空余时间喜欢看些视频,留意到这个动画后就想用代码实现出来,今天整理了下,跟大家分 ...

  9. Spring验证的错误返回------BindingResult

    Spring验证的错误返回------BindingResult 参考资料:http://www.mkyong.com/spring-mvc/spring-mvc-form-errors-tag-ex ...

  10. windows下git bash显示中文

    1.C:\Program Files\Git\etc\git-completion.bash: alias ls='ls --show-control-chars --color=auto' 说明:使 ...