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的更多相关文章

  1. HDU 6071 - Lazy Running | 2017 Multi-University Training Contest 4

    /* HDU 6071 - Lazy Running [ 建模,最短路 ] | 2017 Multi-University Training Contest 4 题意: 四个点的环,给定相邻两点距离, ...

  2. hdu 6071 Lazy Running 最短路建模

    Lazy Running Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others) P ...

  3. HDU 6071 Lazy Running (同余最短路 dij)

    Lazy Running Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)To ...

  4. HDU 6071 Lazy Running (同余最短路)

    Lazy Running Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)To ...

  5. HDU 6071 Lazy Running(很牛逼的最短路)

    http://acm.hdu.edu.cn/showproblem.php?pid=6071 题意: 1.2.3.4四个点依次形成一个环,现在有个人从2结点出发,每次可以往它相邻的两个结点跑,求最后回 ...

  6. 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 之间有路相连, 现在 ...

  7. HDU 6071 Lazy Running (最短路)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=6071 题解 又是一道虐信心的智商题... 首先有一个辅助问题,这道题转化了一波之后就会化成这个问题: ...

  8. 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 ...

  9. HDU 6071 同余最短路 spfa

    Lazy Running Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)To ...

  10. 多校4 lazy running (最短路)

    lazy running(最短路) 题意: 一个环上有四个点,从点2出发回到起点,走过的距离不小于K的最短距离是多少 \(K <= 10^{18} 1 <= d <= 30000\) ...

随机推荐

  1. javascript 模块化 (切记:学习思想)

    模块化(切记:学习思想) 如果不用模块化编写代码,那么会具有以下问题: 代码杂乱无章,没有条理性,不便于维护,不便于复用 很多代码重复.逻辑重复 全局变量污染 不方便保护私有数据(闭包) 模块化的基本 ...

  2. 数据库相关知识积累(sqlserver、oracle、mysql)

    数据库相关知识积累(sqlserver.oracle.mysql) 1. sqlserver :断开所有连接: (还原数据库) 1.数据库  分离 2. USE master GO ALTER DAT ...

  3. InnoSetup 安装选择不同语言,修改软件配置参数,达到安装语言就是软件语言效果

    需求 在软件安装时,选择中英文安装界面,选择的中英文界面就是对应软件内界面语言. 在软件安装时,选择中文界面,打开软件就是中文界面. 在软件安装时,选择英文界面,打开软件就是英文界面. 实际上,就是在 ...

  4. Maven项目 - OpenFeign使用细节 - 从此和httpClient说再见

    maven项目使用openfeign,从此和httpClient说拜拜 pom.xml: <dependency> <groupId>io.github.openfeign&l ...

  5. PHP fread 文件系统函数

    定义和用法 fread - 读取文件(可安全用于二进制文件) 版本支持 PHP4 PHP5 PHP7 支持 支持 支持 语法 fread ( resource $handle , int $lengt ...

  6. JS基本语法---while循环---练习

    JS基本语法---while循环---练习 练习1: 求6的阶乘 var ji = 1;//存储最终的阶乘的结果 var i = 1;//开始的数字 while (i <= 6) { ji *= ...

  7. js延时定时器

    // 获取图片方向延时器 getImageOrientationTimer(context) { if (context.imageTimeout) return; if (context.image ...

  8. E203数据冲突处理OITF

    流水线的数据冲突分为三类:WAR,RAW,WAW https://wenku.baidu.com/view/e066926d48d7c1c708a14508.html WAR: write after ...

  9. 顺F速运,你被爱加M坑了

    - 加密情况 首先我们到顺F官网,下载顺F速运APP,当然,是Android版,毕竟穷. 接下来,得看看怎么用,当然顺便用Wireshark抓包,点那个显眼的立即登录按钮. 使用手机号登录,随便敲敲, ...

  10. Netty高性能组件——FastThreadLocal源码解析(细微处见真章)

    1. 前言 netty自行封装了FastThreadLocal以替换jdk提供的ThreadLocal,结合封装的FastThreadLocalThread,在多线程环境下的变量提高了ThreadLo ...