题目链接:https://cn.vjudge.net/problem/POJ-3169

题意

Farmer John手下的一些牛有自己喜欢的牛,和讨厌的牛

喜欢的牛之间希望距离在给定距离D之内

讨厌的牛之间希望距离在给定距离D之外

每个牛都有一个编号,编号较小的牛要在编号较大的牛之前(坐标可以重叠)

如果不存在这样的队伍,输出-1

如果一号和n号可以随意距离,输出-2

否则输出一号和n号最近距离

思路

首先满足 X_i<X_j

如果两个牛互相喜欢,则有

$ X_i<=X_j+D $ 满足最短路

如果两个牛互相讨厌,则有

$ X_i>=X_j+D $ 经过变形(刚看到的单词subsitution就是这个意思)

$ X_j<=X_i-D$ 满足最短路

然后直接写就好

注意虽然满足编号小的在前,但不要去对dis数组做判断

因为有些节点就没有被更新(没有对应的边)

详情见注释

提交过程

CE Edge没写构造函数
TLE 模板写错了一个符号,导致Bellman没出来哈哈
WA1 对dis数组做了判断
AC1 去掉判断
WA2 怀疑是判断有问题,试了试
AC2 加了行注释

代码

#include <stack>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn=1e3+20, maxm=2e6+20;
const long long INF=1LL<<60;
struct Edge{
int to, dis, next;
Edge(int to=0, int dis=0, int next=0):
to(to), dis(dis), next(next) {}
}edges[maxm*2+5];
int head[maxn+5], size; long long Bellman(int n){
long long dist[maxn+5];
int cnt[maxn+5]={0};
bool inq[maxn+5]={false};
stack<int> sta;// queue<int> que; for (int i=0; i<=n; i++) dist[i]=INF; dist[1]=0;
inq[1]=true;
sta.push(1);// que.push(1);
while (sta.size()){
int from=sta.top(); sta.pop();
inq[from]=false; for (int i=head[from]; i!=-1; i=edges[i].next){
Edge &e=edges[i];
int &to=e.to, &dis=e.dis; if (dist[to]<=dist[from]+(long long)dis) continue;
dist[to]=dist[from]+(long long)dis; if (inq[to]) continue;
sta.push(to); if (++cnt[to]>=n) return -1;
}
} if (dist[n]==INF) return -2;
// for (int i=2; i<=n; i++) // does it work?
// if (dist[i]<dist[i-1]) return -1;
//
// Obviously not, we only need to find a way to the point n
// So there may be same points which value INF
// (But it surely values between dist[i-1] and dist[i+1])
return dist[n];
} void init(void){
memset(head, -1, sizeof(head));
size=0;
} void addEdge(int from, int to, int dis){
edges[size]=Edge(to, dis, head[from]);
head[from]=size++;
} int main(void){
int n, ml, md;
int from, to, dis; init();
scanf("%d%d%d", &n, &ml, &md);
for (int i=0; i<ml; i++){
scanf("%d%d%d", &from, &to, &dis);
if (from>to) swap(from, to);
addEdge(from, to, dis);
}
for (int i=0; i<md; i++){
scanf("%d%d%d", &from, &to, &dis);
if (from<to) swap(from, to);
addEdge(from, to, -dis);
}
printf("%lld\n", Bellman(n)); return 0;
}
Time Memory Length Lang Submitted
422ms 47188kB 1961 C++ 2018-06-06 23:04:35

POJ-3169 Layout 最短路 差分约束的更多相关文章

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

    题目链接:http://poj.org/problem?id=3169 题目大意:n头牛,按编号1~n从左往右排列,可以多头牛站在同一个点,给出ml行条件,每行三个数a b c表示dis[b]-dis ...

  2. POJ 3169 Layout (图论-差分约束)

    Layout Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6574   Accepted: 3177 Descriptio ...

  3. poj 3169 Layout(线性差分约束,spfa:跑最短路+判断负环)

    Layout Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15349   Accepted: 7379 Descripti ...

  4. Candies POJ - 3159 (最短路+差分约束)

    During the kindergarten days, flymouse was the monitor of his class. Occasionally the head-teacher b ...

  5. POJ 3169.Layout 最短路

    Layout Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11612   Accepted: 5550 Descripti ...

  6. POJ 3169 Layout(差分约束+最短路)题解

    题意:有一串数字1~n,按顺序排序,给两种要求,一是给定u,v保证pos[v] - pos[u] <= w:二是给定u,v保证pos[v] - pos[u] >= w.求pos[n] - ...

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

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

  8. POJ 3169 Layout (HDU 3592) 差分约束

    http://poj.org/problem?id=3169 http://acm.hdu.edu.cn/showproblem.php?pid=3592 题目大意: 一些母牛按序号排成一条直线.有两 ...

  9. poj 3169 Layout(差分约束+spfa)

    题目链接:http://poj.org/problem?id=3169 题意:n头牛编号为1到n,按照编号的顺序排成一列,每两头牛的之间的距离 >= 0.这些牛的距离存在着一些约束关系:1.有m ...

随机推荐

  1. CDR X6打了3折,再送魔镜插件,是真的么?

    明人不说暗话,我.要.来.个.小.资.讯. CDR X6打了3折,再送魔镜插件,是真的么?   先来说说CorelDRAW,CorelDRAW众所周知,软件的确不便宜啊,对于个人来说,相当于一个高配苹 ...

  2. Consider defining a bean of type 'XX.XX.XX.XX.mapper.XXMapper' in your configuration.

    今天构建一个springboot 项目,采用mybatis+mysql 然后就出现了这种错误....浪费我半天时间 Description: Field loginLogMapper in com.g ...

  3. C/C++中的段错误(Segmentation fault)[转]

    Segment fault 之所以能够流行于世,是与Glibc库中基本所有的函数都默认型参指针为非空有着密切关系的. 来自:http://oss.lzu.edu.cn/blog/article.php ...

  4. Python学习笔记(5)practice:shopping_cart

    2019-02-27 原代码: money = int(input("请输入金额:")) list = ["phone", "clothes" ...

  5. 【codeforces 630E】A rectangle

    [题目链接]:http://codeforces.com/problemset/problem/630/E [题意] 给你一个矩形的区域; 然后让你统计这个矩形区域内,有多少个正六边形. [题解] 规 ...

  6. 数字签名技术与https

    1,非对称加密技术 非对称加密算法需要两个密钥,公开密钥(publickey)和私有密钥(privatekey):公钥和私钥是成对出现的. 非对称加密例子:B想把一段信息传给A,步骤:1)A把公钥传给 ...

  7. ASP.NET-DropDownListFor绑定model数据

    在ASP.NET中,DropDownListFor绑定model数据比用html的select方便太多了,配合listmode这种模型集合就可以在controller里面直接foreach循环处理li ...

  8. [Google Guava] 2.2-新集合类型

    转自:并发编程网 原文链接:http://ifeve.com/google-guava-newcollectiontypes/ 链接博客其他文章中还有更多的guava其他功能的描述,有空可慢慢看. G ...

  9. WPF中多线程统计拆箱装箱和泛型的运行效率

    WPF中多线程统计拆箱装箱和泛型的执行效率.使用的知识点有泛型.多线程.托付.从样例中能够看到使用泛型的效率至少提升2倍 MainWindow.xaml <Window x:Class=&quo ...

  10. HTTP Status 500 - Request processing failed; nested exception is java.lang.NullPointerException

    HTTP Status 500 - Request processing failed; nested exception is java.lang.NullPointerException type ...