POJ-3169 Layout 最短路 差分约束
题目链接: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 最短路 差分约束的更多相关文章
- POJ 3169 Layout (spfa+差分约束)
题目链接:http://poj.org/problem?id=3169 题目大意:n头牛,按编号1~n从左往右排列,可以多头牛站在同一个点,给出ml行条件,每行三个数a b c表示dis[b]-dis ...
- POJ 3169 Layout (图论-差分约束)
Layout Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6574 Accepted: 3177 Descriptio ...
- poj 3169 Layout(线性差分约束,spfa:跑最短路+判断负环)
Layout Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 15349 Accepted: 7379 Descripti ...
- Candies POJ - 3159 (最短路+差分约束)
During the kindergarten days, flymouse was the monitor of his class. Occasionally the head-teacher b ...
- POJ 3169.Layout 最短路
Layout Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11612 Accepted: 5550 Descripti ...
- POJ 3169 Layout(差分约束+最短路)题解
题意:有一串数字1~n,按顺序排序,给两种要求,一是给定u,v保证pos[v] - pos[u] <= w:二是给定u,v保证pos[v] - pos[u] >= w.求pos[n] - ...
- POJ 3169 Layout (spfa+差分约束)
题目链接:http://poj.org/problem?id=3169 差分约束的解释:http://www.cnblogs.com/void/archive/2011/08/26/2153928.h ...
- POJ 3169 Layout (HDU 3592) 差分约束
http://poj.org/problem?id=3169 http://acm.hdu.edu.cn/showproblem.php?pid=3592 题目大意: 一些母牛按序号排成一条直线.有两 ...
- poj 3169 Layout(差分约束+spfa)
题目链接:http://poj.org/problem?id=3169 题意:n头牛编号为1到n,按照编号的顺序排成一列,每两头牛的之间的距离 >= 0.这些牛的距离存在着一些约束关系:1.有m ...
随机推荐
- 想说再见不容易,win7最新市占率依然超36%
微软正在通过努力让Windows 7用户升级至Windows 10,不过从目前的市占率来看,他们还是要加把劲了. 微软正在通过努力让Windows 7用户升级至Windows 10,不过从目前的市占率 ...
- 排序代码(python,c++) 及 基本算法复杂度
0.导语 本节为手撕代码系列之第一弹,主要来手撕排序算法,主要包括以下几大排序算法: 直接插入排序 冒泡排序 选择排序 快速排序 希尔排序 堆排序 归并排序 1.直接插入排序 [算法思想] 每一步将一 ...
- a标签设置高度不生效问题
<a>是内联元素 必须设置成块级元素block 才能有width和height 不过你可以又定义display:block再定义成display:inline 以避免在IE6下产生BUG ...
- github上Devstack的一些变动,截至8.20
从github下直接clone下来的代码在执行之前须要对一些文件进行改动,否则会出现关于REQUIREMENTS的错误 说明:代码前边是"-"号的,须要删除,代码前边是" ...
- what's new in vc2015
1. 变量和函数的注解提示非常实用.象C#了. 2.CStdioFile升级了,不再须要象 vc2013中,用CStdioFileEx来修复错误了. 3. 发现再写.
- storm-安装
storm有两种操作模式: 本地模式和远程模式.使用本地模式的时候,你能够在你的本地机器上开发測试你的topology, 一切都在你的本地机器上模拟出来; 用远端模式的时候你提交的to ...
- spring boot系统学习(知识点笔记)
一.http的注解配置 1.@SpringBootAplication=@SpringBootConfiguration(其实就是个@Configuration)+@EnableAutoConfigu ...
- android init进程分析 init脚本解析和处理
(懒人近期想起我还有csdn好久没打理了.这个android init躺在我的草稿箱中快5年了.略微改改发出来吧) RC文件格式 rc文件是linux中常见的启动载入阶段运行的文件.rc是run co ...
- hdu 4521 小明系列问题——小明序列(线段树+DP或扩展成经典的LIS)
小明系列问题--小明序列 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) Tot ...
- HDU 3949 XOR(高斯消元搞基)
HDU 3949 XOR pid=3949" target="_blank" style="">题目链接 题意:给定一些数字,问任取几个异或值第 ...