题目链接: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. Ibatis在运行期得到可执行到sql

    环境:oracle-11g ,ibatis-2.0 ,java-1.7 最近因为有个需要是在程序中得到ibatis到sql字符串,即通过以下的ibatis配置得到sql语句 <select id ...

  2. POST和GET详解

    GET和POST Http定义了与服务器交互的不同方法,最基本的方法有4种,分别是GET,POST,PUT,DELETE.URL全称是资源描述符,我们可以这样认为:一个URL地址,它用于描述一个网络上 ...

  3. 51nod 1301 集合异或和(DP)

    因为当\(A<B\)时,会存在在二进制下的一位,满足这一位B的这一位是\(1\),\(A\)的这一位是\(0\). 我们枚举最大的这一位.设为\(x\)吧. 设计状态.\(dp[i][j][1/ ...

  4. [luogu3369] 普通平衡树(splay模板)

    题目描述 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作: 1.插入 xx 数 2.删除 xx 数(若有多个相同的数,因只删除一个) 3.查询 xx 数的排名(排名定义为比 ...

  5. Map的四种遍历方法

    1.取值遍历 for(String key:map.keySet()){ System.out.println("key="+key+"and value=" ...

  6. Qt编程—去掉标题栏和设置窗口透明用法

    学习Qt编程,有时候我们很想做出好看又比较炫的画面,这时就常用到qt上的一些技巧. 这里我以一个小例子来展示qt的这些技巧,此qt编程写的,如图:(去掉标题栏和设置窗口透明后) 代码实现部分: .h文 ...

  7. 排序代码(python,c++) 及 基本算法复杂度

    0.导语 本节为手撕代码系列之第一弹,主要来手撕排序算法,主要包括以下几大排序算法: 直接插入排序 冒泡排序 选择排序 快速排序 希尔排序 堆排序 归并排序 1.直接插入排序 [算法思想] 每一步将一 ...

  8. MySql的replace into 语句

    MySQL REPLACE语句介绍 MySQL的REPLACE语句是一个MySQL扩展于SQL标准的语句. 官方定义:REPLACE works exactly like INSERT, except ...

  9. POJ 3749

    第一道简单密码学的题,太水了,本不打算做,第一道,还是纪念一下. #include <iostream> #include <cstdio> #include <cstr ...

  10. DataTables warning: table id=dataTable - Requested unknown parameter &#39;acceptId&#39; for row 0. For more

    重点内容 DataTables warning: table id=dataTable - Requested unknown parameter 'acceptId' for row 0. For ...