Layout
 

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.

Source

 
 
【题意】
  有 N 头奶牛正在排队,它们的编号为 1 到 N,约翰要给它们安排合适的排队位置,满足以下条
件:
  • 首先,所有奶牛要站在一条直线上。由于是排队,所以编号小的奶牛要靠前,不能让编号大的
奶牛插队。但同一个位置可以容纳多头奶牛,这是因为它们非常苗条的缘故
  • 奶牛喜欢和朋友靠得近点。朋友关系有 F 对,其中第 Ai 头奶牛和第 Bi 头奶牛是第 i 对朋友,
它们的距离不能超过 Ci
  • 奶牛还要和讨厌的同类保持距离。敌对关系有 E 对,其中第 Xi 头奶牛和第 Yi 头奶牛是第 i
敌人,它们的距离不能少于 Zi
  你能否帮助约翰找到一个合理的站位方法,满足所有奶牛的要求,而且让 1 号奶牛和 N 号奶牛间的
距离尽量大?

 
【分析】
  

  啊,当然很快看出是差分约束,然而,呵呵,我以前没弄清楚的后遗症,不知道怎样跑是距离最大的。。
  跑最短路求得就是最大的距离,跑最长路求的就是最小的距离。

代码如下:

 #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
#define INF 0xfffffff
#define Maxn 1010
#define Maxm 22010 struct node
{
int x,y,c,next;
}t[Maxm];int len;
int first[Maxn]; void ins(int x,int y,int c)
{
t[++len].x=x;t[len].y=y;t[len].c=c;
t[len].next=first[x];first[x]=len;
} int n;
int dis[Maxn],cnt[Maxn];
bool inq[Maxn]; queue<int > q; int ffind()
{
memset(dis,,sizeof(dis));
while(!q.empty()) q.pop();
dis[]=;q.push();
while(!q.empty())
{
int x=q.front();
cnt[x]++;
if(cnt[x]>n) return -;
for(int i=first[x];i;i=t[i].next)
{
int y=t[i].y;
if(dis[y]>dis[x]+t[i].c)
{
dis[y]=dis[x]+t[i].c;
if(!inq[y])
{
inq[y]=;
q.push(y);
}
}
}
inq[x]=;q.pop();
}
if(dis[n]>=INF-) return -;
return dis[n];
} int main()
{
int f,e;
scanf("%d%d%d",&n,&f,&e);
len=;
memset(first,,sizeof(first));
for(int i=;i<=f;i++)
{
int x,y,c;
scanf("%d%d%d",&x,&y,&c);
ins(x,y,c);
}
for(int i=;i<=e;i++)
{
int x,y,c;
scanf("%d%d%d",&x,&y,&c);
ins(y,x,-c);
}
for(int i=;i<n;i++) ins(i+,i,);
int ans=ffind();
printf("%d\n",ans);
return ;
}

[POJ 3169]


认真的说一下差分约束的理解??

???

先说最短路吧,其实用最短路啊最长路啊都是可以的,但是我不会告诉你我就是在这里被坑到。

最短路的话,把所有约束都化成x-y<=k的形式,然后add(y,x,k)就可以了,这样跑最短路保证d[x]<=d[y]+k。

如果差分约束有解,那么一定有无穷解,因为我们可以把所有值同时加上k,所有约束同样成立。

所以问题往往是某两个数的差值的最大值 或者 最小值。

可以证明,跑最短路的话,d[x]一定是与d[st]的差值最大的

因为你的约束都是x-y<=k的形式,如果x与st联通,那么肯定是有几个表达式x-a1<=k1,a1-a2<=k2,...,ax-st<=kx,

把他们全部加起来有x-st<=xxx,我们用类似xxx的值建边的,所以求出来的一定是满足条件的最大的x。

在这个时候,如果你要求最小的x,那么,你就把x作为起点,st作为终点跑最短路,求出来的就是最小的差值的相反数

最长路刚好反过来啦,化成x-y>=k的形式,add(y,x,k),则保证d[x]>=d[y]+k。

这时d[x]一定是与d[st]的差值最小的

反转起点终点跑,就是得到最大的差值的相反数。

好了,还有无解的情况。

如果用最短路,图中有负环,则无解。 因为你负环,说明有一些约束相加得到:x-k<=x,并且k是负数,显然不成立,so,无解。

同理,如果用最长路,图中有正环,也无解。因为这说明有约束的和为x-k>=x,且k是整数。

好像目前就知道这么多啦~~

如果你要求每个点都是非负数的情况下,某个点的最小非负值怎么破?

add一个源点,它的值为0,与他的差值就是那个数的真正的值。

然后就有约束 d[1]-d[st]>=0 d[2]-d[st]>=0...d[n]-d[st]>=0

然后就求d[x]与d[st]的最小差值。

这种问法貌似很常见??

2016-10-21 18:04:51

【POJ3169 】Layout (认真的做差分约束)的更多相关文章

  1. S - Layout (最短路&&差分约束)

    Like everyone else, cows like to stand close to their friends when queuing for feed. FJ has N (2 < ...

  2. ShortestPath:Layout(POJ 3169)(差分约束的应用)

                布局 题目大意:有N头牛,编号1-N,按编号排成一排准备吃东西,有些牛的关系比较好,所以希望他们不超过一定的距离,也有一些牛的关系很不好,所以希望彼此之间要满足某个关系,牛可以 ...

  3. bzoj 1731: [Usaco2005 dec]Layout 排队布局【差分约束】

    差分约束裸题,用了比较蠢的方法,先dfs_spfa判负环,再bfs_spfa跑最短路 注意到"奶牛排在队伍中的顺序和它们的编号是相同的",所以\( d_i-d_{i-1}>= ...

  4. 图论--差分约束--POJ 3169 Layout(超级源汇建图)

    Like everyone else, cows like to stand close to their friends when queuing for feed. FJ has N (2 < ...

  5. poj 3169 Layout (差分约束)

    3169 -- Layout 继续差分约束. 这题要判起点终点是否连通,并且要判负环,所以要用到spfa. 对于ML的边,要求两者之间距离要小于给定值,于是构建(a)->(b)=c的边.同理,对 ...

  6. ☆ [POJ1021] Intervals 「差分约束」

    传送门 >Here< 题意:给出N段区间,并告诉你每段区间里有几个数(一个位置只能放一个数) 问总共至少有几个数 解题思路 差分约束题,本蒟蒻也是第一次做差分约束题…… 所谓差分约束,常常 ...

  7. ACM差分约束笔记

    https://www.cnblogs.com/31415926535x/p/10463112.html 很早之前学最短路的时候就看了一眼差分约束,,当时以为这种问题不怎么会出现,,而且当时为了只为了 ...

  8. BZOJ4383 [POI2015]Pustynia[线段树优化建边+拓扑排序+差分约束]

    收获挺大的一道题. 这里的限制大小可以做差分约束,从$y\to x$连$1$,表示$y\le x-1$即$y<x$,然后跑最长路求解. 但是,如果这样每次$k+1$个小区间每个点都向$k$个断点 ...

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

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

随机推荐

  1. linux-ssh远程后台执行脚本-放置后台执行问题(转)

    写了一个监控负载的小脚本(死循环,测试结束后再kill对应进程),因需要监控多台服务器,所以在一台服务器上使用ssh统一执行脚本遇到问题:使用ssh root@172.16.146.20 '/usr/ ...

  2. .Net Framework 各个版本新特性总结 (一)

    .Net Framework 4.5 新特性 最近面试时又看到有问.Net Framework 新特性的问题,一时被问到了.平时也是拿起来就用,新版本出来了,新特性也就是瞄一眼,也没去仔细查看.这次干 ...

  3. lsjORM ----让开发变得更加快捷(二)

    lsjORM结构 跟传统三层没有多大区别,这里添加DTO(参数列表)跟PetaPoce(数据库操作),普通的三层我们都喜欢用DBHelper或者SqlHelper来封装sql的辅助方法,PetaPoc ...

  4. Think in java浏览一

    Think in java作为java语言的圣经书籍之一,几乎成为每个java程序员必看的书籍,不看都不好意思说自己是java程序员,不过一般也不说自己认真看了,就说自己翻了翻.作为写安卓的,当然也要 ...

  5. Highcharts在IE中不能一次性正常显示的一种解决办法

    由于客户要求必须在IE浏览器下兼容图表,故选用了兼容性较好的Highcharts.另外说一句,博主尝试过ichartjs.ECharts.YUI,兼容性都没有Highcharts给力(所有的兼容性问题 ...

  6. SetTimer的使用

    SetTimer函数用于创建一个计时器,KillTimer函数用于销毁一个计时器.计时器属于系统资源,使用完应及时销毁. SetTimer的函数原型如下:UINT_PTR SetTimer( HWND ...

  7. SQL的update from 理解

    学习了sql的语句都有快3年,工作上使用都一年半的,最近突然想起update from语句,感觉好像很模糊,虽然语法上使用一直正确,一直都是这样使用,但是就好像不是很明白里面的深处意思. 今天特意测试 ...

  8. 巧谈 GCD

    转载自:http://www.jianshu.com/p/665261814e24 谈到iOS多线程,一般都会谈到四种方式:pthread.NSThread.GCD和NSOperation.其中,苹果 ...

  9. 抓取Bing每日图片作为网站首页背景

    把Bing搜索的背景图片设置为自己网站的背景,实现背景及资讯的每日更新 效果图如下: 理一下思路,首先我们要抓取Bing的每日图片及最新资讯,然后保存图片及信息到本地,最后显示图片及资讯到网站首页. ...

  10. Cocos2d-x 3.0 cocostudio骨骼动画的动态换肤

    概述 游戏中人物的状态会发生改变,而这种改变通常要通过局部的变化来表现出来.比如获得一件装备后人物形象的改变,或者战斗中武器.防具的损坏等.这些变化的实现就要通过动态换肤来实现.在接下来的这个Demo ...