POJ 3169 Layout (spfa+差分约束)】的更多相关文章

题目链接:http://poj.org/problem?id=3169 差分约束的解释:http://www.cnblogs.com/void/archive/2011/08/26/2153928.html 我也不是特别理解,要是给你a - b <= k 就建一条b->a权值为k的有向边,要是a - b >= k 就建一条a -> b边权是-k的有向边,要是让你求n到1的最大差,就是让你求1到n的最短距离. 差分约束系统有两种方式可以求解,最短路和最长路.当我们把不等式整理成d[a…
题目链接:http://poj.org/problem?id=3169 题意:n头牛编号为1到n,按照编号的顺序排成一列,每两头牛的之间的距离 >= 0.这些牛的距离存在着一些约束关系:1.有ml组(u, v, w)的约束关系,表示牛[u]和牛[v]之间的距离必须 <= w.2.有md组(u, v, w)的约束关系,表示牛[u]和牛[v]之间的距离必须 >= w.问如果这n头无法排成队伍,则输出-1,如果牛[1]和牛[n]的距离可以无限远,则输出-2,否则则输出牛[1]和牛[n]之间的最…
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 numbe…
<题目链接> 题目大意: 一些母牛按序号排成一条直线.有两种要求,A和B距离不得超过X,还有一种是C和D距离不得少于Y,问可能的最大距离.如果没有最大距离输出-1,如果1.n之间距离任意就输出-2,否则输出最大的距离. 解题分析:典型的差分约束,第一种约束,v-u<=c:第二种约束:v-u>=c  可以转化为 u-v<=c ,还有一种约束,因为题目要求按序号排序,所以 loc[i] - loc[i-1]>=0 ,即 loc[i-1]-loc[i]<=0 .以上就是…
3169 -- Layout 继续差分约束. 这题要判起点终点是否连通,并且要判负环,所以要用到spfa. 对于ML的边,要求两者之间距离要小于给定值,于是构建(a)->(b)=c的边.同理,对于MD的,构建(b)->(a)=-c的边.然后就是(i+1)->(i)=0,两者距离大于0的限制. 代码如下: #include <cstdio> #include <iostream> #include <algorithm> #include <cst…
题意:有一串数字1~n,按顺序排序,给两种要求,一是给定u,v保证pos[v] - pos[u] <= w:二是给定u,v保证pos[v] - pos[u] >= w.求pos[n] - pos[1]最大,若无解输出-1,无穷多解输出-2. 思路:光看题目好像和最短路无关,其实这里用到了spfa的松弛操作来保证所给出的两种要求.若pos[v] - pos[u] >= w,则pos[v] +(- w) >=  pos[u],也就是pos[v] +(- w) < pos[u]时进…
Layout Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6549   Accepted: 3168 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…
题面 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…
Layout 题目链接: Rhttp://acm.hust.edu.cn/vjudge/contest/122685#problem/S 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 waitin…
O - Layout(差分约束 + spfa) 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 the…