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. windows组件

    Wcript.shell是windows自带的组件 因为他过于强大 所以经常被黑客加以利用 他可以调用系统内核运行的dos基本命令 与此对应的还有三个危险组件 他们分别是 FSO shell.Appl ...

  2. Spring Mvc 在非controller层 实现获取request对象

    一般我们在Controller层,会编写类似这样的方法 @Controller @RequestMapping(value="/detail") public class GetU ...

  3. 获取元素在浏览器中的绝对位置(从jquery1.8中抠出来)

    <style> html,body{margin:0;padding:0;} .d1{margin-left:40px;background:red;width:2000px;height ...

  4. iOS之block

    1. Block的声明和线程安全Block属性的声明,首先需要用copy修饰符,因为只有copy后的Block才会在堆中,栈中的Block的生命周期是和栈绑定的,可以参考之前的文章(iOS: 非ARC ...

  5. 022医疗项目-模块二:药品目录的导入导出-对XSSF导出excel类进行封装

    资源全部来源于传智播客. 好的架构师写的程序,就算给刚入门的新手看,新手一看就知道怎么去用.所以我们要对XSSF导出excel类进行封装.这是架构师的工作,但我们也要知道. 我们写一个封装类: 这个类 ...

  6. C# 改变无边框窗体的尺寸大小

    以下代码为修改窗体尺寸的代码: const int HTLEFT = 10; ; ; ; ; ; const int HTBOTTOMLEFT = 0x10; ; protected override ...

  7. 求时间差的sql语句。 比如如下数据

    msisdn createtime closetime138 2011-5-17 15:30:00:000 2011-5-17 15:30:00:530138 2011-5-17 15:40:00:0 ...

  8. JS案例之1——pager 分页

    学习JS大半年之久,第一次自己尝试写一些小插件,写法参考网上某位牛人写代码的思路. 此处代码写的是静态分页.如果需动态分页,还可以修改下.第一次写,还有很多地方可以优化.希望各位大牛踊跃拍砖. 预览图 ...

  9. 从0开始学Java——@override的作用

    早上跟着<jsp&Servlet学习笔记>来学习jsp,在使用eclipse创建了一个servlet类之后,发现自动创建的类和书上相比,doGet方法的前面少了@override, ...

  10. Week2学习过程报告

    一.学习内容 1. 熟悉Linux系统下的开发环境   2. 熟悉vi的基本操作   3. 熟悉gcc编译器的基本原理   4. 熟练使用gcc编译器的常用选项   5 .熟练使用gdb调试技术    ...