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 ...
随机推荐
- CorelDRAW X6冰点价加推800套燃爆6月
CorelDRAW是迄今最具创意的图形设计程序,并获得所有行业的公认的知名工具.在刚刚结束的CorelDRAW X6限量活动之后,CorelDRAW官方继续加推800套CorelDRAW X6满足用户 ...
- node——request和response的常用对象
request(http.IncomingMessage)和response(http.ServerResponse)对象介绍 request:服务器解析用户提交的http请求报文,将结果解析到req ...
- kafka内外网集群配置
linux下配置使用以第一台为例(先配置好jdk环境)1.解压kafka:2.10-0.10.1.12.修改zookeeper.properties 新增配置:maxClientCnxns=0 tic ...
- php版本过低错误导致的laravel 错误:Illuminate\Foundation\helpers.php on line 233; syntax error, unexpected '?'
今天运行laravel项目发现出现错误: Parse error: syntax error, unexpected '?' ..\vendor\laravel\framework\src\Illu ...
- LeetCode 856 递归思路详解
题目描述 给定一个平衡括号字符串 S,按下述规则计算该字符串的分数: () 得 1 分. AB 得 A + B 分,其中 A 和 B 是平衡括号字符串. (A) 得 2 * A 分,其中 A 是平衡括 ...
- 转载一遍比较好的,django2.1搭建博客教程
非常感谢这位博主,找了几个星期终于找到了 https://www.dusaiphoto.com/article/article-detail/4/
- input输入框获取焦点是背景黄色去除
input:-webkit-autofill { -webkit-box-shadow: 0 0 0px 1000px white inset !important;}
- Css学习总结(2)——60个有用CSS代码片段
1.垂直对齐 如果你用CSS,则你会有困惑:我该怎么垂直对齐容器中的元素?现在,利用CSS3的Transform,可以很优雅的解决这个困惑: .verticalcenter{ position: re ...
- C#-委托 lambda 匿名方法 匿名类型
1.lambda 匿名方法 匿名类型 delegate void d1(); d1 d = delegate(){Console.WriteLine("this is a test" ...
- 洛谷 P3420 [POI2005]SKA-Piggy Banks
P3420 [POI2005]SKA-Piggy Banks 题目描述 Byteazar the Dragon has NN piggy banks. Each piggy bank can eith ...