Lazy Running

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 101    Accepted Submission(s): 40

Problem Description

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

Input

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

Output

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

Source

2017 Multi-University Training Contest - Team 4

比赛的时候就是蠢……

当时只剩下十几分钟的时候看到这题的……其实很简单的一道最短路,前提是想到了……

题目要求是只有四个点,然后连边成正方形,问从2号点出发,再回到2号点且走过的总距离大于K的最短的路径是多少。对于这个,我们考虑如果存在一条合法路径,那么我再走2*w也一定是合法路径,其中w表示与2相连的某条边的长度。即回到2之后再出去再回来,这样的路径一定合法。那么,如果走了某条路径回到了2,然后总距离不够的话,我们只需要加上几个2*w使得最后结果大于等于K即可。

那么如何使这个结果最小呢?我们考虑设置一个数组d[x][p]表示从2出发最后到达x点且费用对2*w取模结果为p时的最小花费。这样子原本的一个点就可以拆成2*w个点,这样跑一遍dijkstra即可求出d数组。之后,对于每一个对2*w取模后的数值,我们都可以计算把它补到大于K且距离K最近的花费,再在这些花费中取一个最小的即可。那么,这样子考虑为什么可以包含全部的而且最优的解呢?因为我们最后补的是2*w或者它的倍数,然后我把对2*w取模后的所有取值的最小花费都计算了一次,这样子得出来的最小花费一定包含所有的情况,即2*w的所有剩余系都被包括了,所以可以保证正确性。具体代码如下:

转载于http://blog.csdn.net/yasola/article/details/76684704;

  1. #include <iostream>
  2. #include <algorithm>
  3. #include <cstdio>
  4. #include <cstring>
  5. #include <cstdlib>
  6. #include <cmath>
  7. #include <ctime>
  8. #include <vector>
  9. #include <queue>
  10. #include <stack>
  11. #include <deque>
  12. #include <string>
  13. #include <map>
  14. #include <set>
  15. #include <list>
  16. using namespace std;
  17. #define INF 0x3f3f3f3f3f3f3f3f
  18. #define LL long long
  19. #define fi first
  20. #define se second
  21. #define mem(a,b) memset((a),(b),sizeof(a))
  22.  
  23. const int MAXN=+;
  24. const int MAXM=+;
  25.  
  26. struct Node
  27. {
  28. int p;
  29. LL dis;
  30. Node(int p, LL d):p(p), dis(d){}
  31. };
  32.  
  33. LL K, G[MAXN][MAXN], m, ans;
  34. bool vis[MAXN][MAXM];
  35. LL dist[MAXN][MAXM];//从1开始到达i,模m等于j的最短路
  36.  
  37. void spfa(int s)
  38. {
  39. queue<Node> que;
  40. mem(vis, );
  41. mem(dist, 0x3f);
  42. que.push(Node(, ));
  43. dist[][]=;
  44. vis[][]=true;
  45. while(!que.empty())
  46. {
  47. int u=que.front().p;
  48. LL now_dis=que.front().dis;
  49. vis[u][now_dis%m]=false;
  50. que.pop();
  51. for(int i=-;i<;i+=)
  52. {
  53. int v=(u+i+)%;
  54. LL next_dis=now_dis+G[u][v], next_m=next_dis%m;
  55. if(v==s)//形成环,更行答案
  56. {
  57. if(next_dis<K)
  58. ans=min(ans, next_dis+((K-next_dis-)/m+)*m);
  59. else ans=min(ans, next_dis);
  60. }
  61. if(dist[v][next_m]>next_dis)
  62. {
  63. dist[v][next_m]=next_dis;
  64. if(!vis[v][next_m])
  65. {
  66. que.push(Node(v, next_dis));
  67. vis[v][next_m]=true;
  68. }
  69. }
  70. }
  71. }
  72. }
  73.  
  74. int main()
  75. {
  76. int T_T;
  77. scanf("%d", &T_T);
  78. while(T_T--)
  79. {
  80. scanf("%lld", &K);
  81. for(int i=;i<;++i)
  82. {
  83. scanf("%lld", &G[i][(i+)%]);
  84. G[(i+)%][i]=G[i][(i+)%];
  85. }
  86. m=*min(G[][], G[][]);//最小环
  87. ans=((K-)/m+)*m;//只使用最短的回路
  88. spfa();
  89. printf("%lld\n", ans);
  90. }
  91.  
  92. return ;
  93. }

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 (同余最短路 dij)

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

  3. hdu 6071 Lazy Running 最短路建模

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

  4. HDU 6071 Lazy Running (最短路)

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

  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 同余最短路 spfa

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

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

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

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

随机推荐

  1. 【转】js JavaScript 的性能优化:加载和执行

    JavaScript 的性能优化:加载和执行 转自:https://www.ibm.com/developerworks/cn/web/1308_caiys_jsload/ 随着 Web2.0 技术的 ...

  2. Sass 条件-循环语句

    学习Sass中 @if...@else @for @while @each 一.条件判断 - @if @else 示例: @mixin blockOrHidden($boolean:true){ @i ...

  3. CentOS 7 vim显示中文乱码

    使用xshell的时候,发现有时候中文显示有乱码,一开始以为是Xshell没设置好,后来检查了一下xshell<<文件<<属性<<终端:右侧编码,显示的是Unico ...

  4. Informatica _组件使用介绍及优化

    转载 http://blog.csdn.net/yongjian1092/article/details/52588434 有空自己会写一个关于这方面的文章.

  5. 多线程---iOS-Apple苹果官方文档翻译

    本系列所有开发文档翻译链接地址:iOS7开发-Apple苹果iPhone开发Xcode官方文档翻译PDF下载地址(2013年12月29日更新版)   多线程  技术博客http://www.cnblo ...

  6. 01背包入门 dp

    题目引入: 有n个重量和价值分别为Wi,Vi的物品.从这些物品中挑选出总重量不超过W的物品,求所有挑选方案中的价值总和的最大值. 分析: 首先,我们用最普通的方法,针对每个物品是否放入背包进行搜索. ...

  7. arch中pacman的使用

    Pacman 是archlinux 下的包管理软件.它将一个简单的二进制包格式和易用的构建系统结合了起来.不管软件包是来自官方的 Arch 库还是用户自己创建,Pacman 都能方便得管理. pacm ...

  8. python科学计算整理

    网站: http://bokeh.pydata.org/gallery.html

  9. 很重要的处理项目url[www]

    http://www.xdowns.com/soft/10/57/2013/Soft_113319.html https://github.com/TricksterGuy/Morphan http: ...

  10. pandas+sqlalchemy 保存数据到mysql

    import pandas as pd from sqlalchemy import create_engine data3={"lsit1":[1,2],"lsit2& ...