hdu-6071 Lazy Running
In HDU, you have to run along the campus for 24 times, or you will fail in PE. According to the rule, you must keep your speed, and your running distance should not be less than K meters.
There are 4 checkpoints in the campus, indexed as p1,p2,p3 and p4.
Every time you pass a checkpoint, you should swipe your card, then the
distance between this checkpoint and the last checkpoint you passed will
be added to your total distance.
The system regards these 4 checkpoints as a circle. When you are at checkpoint pi, you can just run to pi−1 or pi+1(p1 is also next to p4). You can run more distance between two adjacent checkpoints, but only the distance saved at the system will be counted.
Checkpoint p2
is the nearest to the dormitory, Little Q always starts and ends
running at this checkpoint. Please write a program to help Little Q find
the shortest path whose total distance is not less than K
InputThe first line of the input contains an integer T(1≤T≤15), denoting the number of test cases.
In each test case, there are 5 integers K,d1,2,d2,3,d3,4,d4,1(1≤K≤1018,1≤d≤30000), denoting the required distance and the distance between every two adjacent checkpoints.OutputFor each test case, print a single line containing an integer, denoting the minimum distance.Sample Input
1
2000 600 650 535 380
Sample Output
2165
Hint
The best path is 2-1-4-3-2.
OJ-ID:
hdu-6071
author:
Caution_X
date of submission:
20191024
tags:
dp+dijkstra
description modelling:
有四个点1,2,3,4形成环1-2-3-4-1,四条边的权值表示距离,问能否找到一条路径从点2出发回到点2并且权值之和大于K
major steps to solve it:
w=min(d1,d2),其中d1是1,2之间的距离,d2是2,3之间的距离
假设有符合条件的最小权值和P,那么就会有最佳的P-w
设dp[i][j]:=从2出发到达i点时的最小权值和,其中j是权值和%(2*w),之所以%(2*w)是因为从2出发再回到2权值和最少2*w
同样是从2到达i点,不同的j表示不同的路径,若j相同但是从2到i的权值和不同,则选择权值和小的
(1)dijkstra更新dp[i][j]
(2)判断dp[2][j]是否大于K,若小于K则不足之处用w填充
AC code:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, int> P;
const ll INF = 1e18;
const int MAXN = 6e4 + ;
vector<P> G[MAXN];
ll d[][MAXN];
void dijkstra(ll w, int s) {
for(int i = ; i <= ; i++) {
fill(d[i], d[i] + w, INF);
}
priority_queue<P, vector<P>, greater<P> >q;
q.push(P(, s));
d[s][] = ;
while(!q.empty()) {
P p = q.top(); q.pop();
int v = p.second;
if(p.first > d[v][p.first % w]) continue;
for(int i = ; i < (int)G[v].size(); i++) {
P e = G[v][i];
ll dist = e.first + d[v][p.first % w];
if(dist < d[e.second][dist % w]) {
d[e.second][dist % w] = dist;
q.push(P(dist, e.second));
}
}
}
}
int main() {
int T;
cin >> T;
while(T--) {
memset(G, , sizeof G);
ll K, d1, d2, d3, d4;
cin >> K >> d1 >> d2 >> d3 >> d4;
G[].push_back(P(d1, ));
G[].push_back(P(d1, ));
G[].push_back(P(d2, ));
G[].push_back(P(d2, ));
G[].push_back(P(d3, ));
G[].push_back(P(d3, ));
G[].push_back(P(d4, ));
G[].push_back(P(d4, ));
ll w = * min(d1, d2);
dijkstra(w, );
ll ans = INF;
for(ll i = ; i < w; i++) {
if(d[][i] >= K) {
ans = min(ans, d[][i]);
} else {
ll nd = K - d[][i];
ans = min(ans, d[][i] + nd / w * w + (nd % w > ) * w);
}
}
cout << ans << endl;
}
return ;
}
hdu-6071 Lazy Running的更多相关文章
- HDU 6071 - Lazy Running | 2017 Multi-University Training Contest 4
/* HDU 6071 - Lazy Running [ 建模,最短路 ] | 2017 Multi-University Training Contest 4 题意: 四个点的环,给定相邻两点距离, ...
- hdu 6071 Lazy Running 最短路建模
Lazy Running Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others) P ...
- HDU 6071 Lazy Running (同余最短路 dij)
Lazy Running Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)To ...
- HDU 6071 Lazy Running (同余最短路)
Lazy Running Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)To ...
- HDU 6071 Lazy Running(很牛逼的最短路)
http://acm.hdu.edu.cn/showproblem.php?pid=6071 题意: 1.2.3.4四个点依次形成一个环,现在有个人从2结点出发,每次可以往它相邻的两个结点跑,求最后回 ...
- HDU 6071 Lazy Running(最短路)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=6071 [题目大意] 给出四个点1,2,3,4,1和2,2和3,3和4,4和1 之间有路相连, 现在 ...
- HDU 6071 Lazy Running (最短路)
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=6071 题解 又是一道虐信心的智商题... 首先有一个辅助问题,这道题转化了一波之后就会化成这个问题: ...
- 2017 Multi-University Training Contest - Team 4 hdu6071 Lazy Running
地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=6071 题目: Lazy Running Time Limit: 2000/1000 MS (J ...
- HDU 6071 同余最短路 spfa
Lazy Running Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)To ...
- 多校4 lazy running (最短路)
lazy running(最短路) 题意: 一个环上有四个点,从点2出发回到起点,走过的距离不小于K的最短距离是多少 \(K <= 10^{18} 1 <= d <= 30000\) ...
随机推荐
- java在hashmap初始化时赋初值
Java中的HashMap是一种常用的数据结构,一般用来做数据字典或者Hash查找的容器. 一般我们初始化并赋初值是这样做的: HashMap<String, Object> map = ...
- Python使用数字与字符串的技巧
1.少写数字字面量 "数字字面量(integer literal)" 是指那些直接出现在代码里的数字.它们分布在代码里的各个角落,比如代码 del users[0] 里的 0 就是 ...
- mysql 是否走索引问题
问题探讨 : 当一列包含null 值时, is null 和 is not null 查询是否走索引 当用 != 时是否走索引 当用in时是否走索引 结论:当 查询范围比较小时, 以上枚举的都走索 ...
- crm-4权限
1.rbac-优化login函数 因为login是业务逻辑 ,而rbac是个组件 ,将rbac在login的代码分离 ###初始化权限函数分离出去 rbac/service/permission fr ...
- Android多module下重复jar包问题
版权声明:本文为xing_star原创文章,转载请注明出处! 本文同步自http://javaexception.com/archives/166 Android多module下重复jar包问题 An ...
- Android监视器概述
还望支持个人博客站:http://www.enjoytoday.cn Android监视器可帮助您分析应用程序的性能,以便您优化,调试和改进它们. 它可以让您从硬件设备或Android模拟器监控应用程 ...
- 【LeetCode】650. 只有两个键的键盘
只有两个键的键盘 最初在一个记事本上只有一个字符 'A'.你每次可以对这个记事本进行两种操作: 1.Copy All (复制全部) : 你可以复制这个记事本中的所有字符(部分的复制是不允许的). 2. ...
- Java垃圾收集器——Parallel、G1收集器日志分析及性能调优示范
开发过程中,经常需要对GC的垃圾收集器参数不断的进行动态调整,从而更充分的压榨机器性能,提升应用效率.本文将从常见的Parallel/G1垃圾收集器的GC日志着手,分析GC日志的具体含义,以及示范如何 ...
- Angular 学习笔记(二)
控制器: 就像 JavaScript 里的构造函数一般,用来增强作用域(scope),当一个控制器通过 ng-controller 指令来添加到 DOM 中时, ng 会调用该控制器的构造函数来生成一 ...
- 网络流媒体协议之——RTSP协议
RTSP(Real-Time Stream Protocol)协议是一个基于文本的多媒体播放控制协议,属于应用层.RTSP以客户端方式工作,对流媒体提供播放.暂停.后退.前进等操作.该标准由IETF指 ...