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\) ...
随机推荐
- php中trait的使用方法
1.php中的trait是啥? 看上去既像类又像接口,其实都不是,Trait可以看做类的部分实现,可以混入一个或多个现有的PHP类中,其作用有两个:表明类可以做什么:提供模块化实现.Trait是一种代 ...
- CentOS 7 firewalld详解,添加删除策略
一.CentOS7中firewall防火墙 修改防火墙配置文件之前,需要对之前防火墙[/etc/firewalld/zones/public.xml]做好备份 重启防火墙后,需要确认防火墙状态和防火墙 ...
- Java队列和定时器Timer
一: Queue详解 Queue: 基本上,一个队列就是一个先入先出(FIFO)的数据结构 Queue接口与List.Set同一级别,都是继承了Collection接口.Linked ...
- Mysql - 存储过程 - 定时删表
在工业监控里面, 需要对每天的数据, 进行记录, 时间长了之后, 数据库很容易撑爆. 这时候, 如果允许, 可以对之前的数据进行一次清除, 只记录几个月内的数据. delimiter $ DROP P ...
- javascript实现base64编码、解码
我们知道,浏览器的window对象提供有window.atob()和window.btoa()方法可以对字符串进行Base64编码和解码. console.log(window.btoa(window ...
- 由 ToString()和Convert.ToString() 引发的问题
对于久经沙场的程序猿来说,类型转换再熟悉不过了,在代码中我们也会经常用到. 前几天,有个学生问我关于类型转换ToString()和Convert.ToString()的区别,这么常用的东西我竟然支支吾 ...
- java基础(8):Eclipse开发工具
1. Eclipse开发工具 Eclipse是功能强大Java集成开发工具.它可以极大地提升我们的开发效率.可以自动编译,检查错误.在公司中,使用的就是Eclipse进行开发. 1.1 Eclipse ...
- struts图片上传
文件上传:三种上传方案1.上传到tomcat服务器 上传图片的存放位置与tomcat服务器的耦合度太高2.上传到指定文件目录,添加服务器与真实目录的映射关系,从而解耦上传文件与tomcat的关系文件服 ...
- Linux帮助——常用命令
Linux帮助——常用命令 摘要:本文主要学习了Linux系统中常用的一些命令. uname命令 uname命令可以显示电脑以及操作系统的相关信息. 基本语法 uname [选项] 选项说明 -a:显 ...
- 事务的四大性质:ACID
1. 原子性(Atomicity) 一个原子事务要么完整执行,要么干脆不执行.这意味着,工作单元中的每项任务都必须正确执行.如果有任一任务执行失败,则整个工作单元或事务就会被终止.即此前对数据所作的任 ...