Lazy Running

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

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

 
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
【题意】四个点连成环,相邻两个点之间有距离,问从点 1 出发回到点1 ,总距离超过K 的最短路是多少。
【分析】像这种无限走下去的题,可以用同余最短路来解。点1相邻的两条边,设最短的那条长度为,m,那么存在一条长度为x的回到1节点的路,就一定存在长度为x+2*m的路。
  dis[i][j]表示到达i点总长度%2*m==j的最短路,然后dij就行了。

  1. #include <bits/stdc++.h>
  2. #define inf 0x3f3f3f3f
  3. #define met(a,b) memset(a,b,sizeof a)
  4. #define pb push_back
  5. #define mp make_pair
  6. #define rep(i,l,r) for(int i=(l);i<=(r);++i)
  7. #define inf 0x3f3f3f3f
  8. using namespace std;
  9. typedef long long ll;
  10. const int N = 6e4+;;
  11. const int M = ;
  12. const int mod = ;
  13. const int mo=;
  14. const double pi= acos(-1.0);
  15. typedef pair<int,int>pii;
  16. typedef pair<ll,int>P;
  17. int n,s;
  18. ll dis[][N];
  19. ll k,edg[][],m,ans;
  20. void dij(int s){
  21. priority_queue<P,vector<P>,greater<P> >q;
  22. for(int i=;i<;i++){
  23. for(int j=;j<=m;j++){
  24. dis[i][j]=1e18;
  25. }
  26. }
  27. q.push(P(0LL,s));
  28. while(!q.empty()){
  29. ll w=q.top().first;
  30. int u=q.top().second;
  31. q.pop();
  32. if(u==s){
  33. if(w<k){
  34. ans=min(ans,w+((k-w-)/m+)*m);
  35. }
  36. else ans=min(ans,w);
  37. }
  38. for(int i=;i<;i++){
  39. if(!edg[u][i])continue;
  40. ll d=w+edg[u][i];
  41. if(dis[i][d%m]>d){
  42. dis[i][d%m]=d;
  43. q.push(P(d,i));
  44. }
  45. }
  46. }
  47. }
  48. int main(){
  49. int T;
  50. scanf("%d",&T);
  51. while(T--){
  52. ans=1e18;
  53. scanf("%lld",&k);
  54. for(int i=;i<;i++){
  55. scanf("%lld",&edg[i][(i+)%]);
  56. edg[(i+)%][i]=edg[i][(i+)%];
  57. }
  58. m=*min(edg[][],edg[][]);
  59. ans=((k-)/m+)*m;
  60. dij();
  61. printf("%lld\n",ans);
  62. }
  63. return ;
  64. }

HDU 6071 Lazy Running (同余最短路 dij)的更多相关文章

  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)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. 构造+分块思想 Codeforces Round #319 (Div. 1) C

    http://codeforces.com/contest/576/problem/C 题目大意: 给你一个曼哈顿距离的图,然后要求你找到一个链,链穿了所有的点 然后要求这链的长度<=25*10 ...

  2. 在vm上面安装Linux系统

    1 在vm上面安装Linux系统 1  以管理员的身份运行VMware:  点击VM图标然后右键属性 ,点兼容性 ---特权 等级 选择 以管理员的身份运行此软件          2 . 添加一个虚 ...

  3. 关于Java泛型深入理解小总结

    1.何为泛型 首先泛型的本质便是类型参数化,通俗的说就是用一个变量来表示类型,这个类型可以是String,Integer等等不确定,表明可接受的类型,原理类似如下代码 int pattern; //声 ...

  4. 如何阻止自动更新‘updated_at’和'created_at'

    可以在模版中添加一条代码: public $timestamps = false;

  5. 配置node,sass,淘宝镜像环境

    由于最近由于刚到手一台新的thinkpad(哈哈,宝宝是个小穷B,木有小苹果),所以工作开发中所用到的环境就需要重新安装一下啦,这里的话,我就把我目前所用到的进行总结一下,其余的会在以后的开发过程中, ...

  6. 爬虫实战--基于requests 和 Beautiful的7160美图网爬取图片

    import requests import os from bs4 import BeautifulSoup import re # 初始地址 all_url = 'http://www.7160. ...

  7. linux——vi和vim的区别

    vi 和vim 的区别   它们都是多模式编辑器,不同的是vim 是vi的升级版本,它不仅兼容vi的所有指令,而且还有一些新的特性在里面. vim的这些优势主要体现在以下几个方面:1.多级撤消我们知道 ...

  8. SSL handshake failed: SSL error: Key usage violation in certificate has been detected.

    sudo apt-get install libneon27-dev cd /usr/libsudo mv libneon-gnutls.so.27 libneon-gnutls.so.27.olds ...

  9. 「caffe编译bug」python/caffe/_caffe.cpp:10:31: fatal error: numpy/arrayobject.h: No such file or directory

    在Makefile.config找到PYTHON_INCLUDE,发现有点不同: PYTHON_INCLUDE := /usr/include/python2.7 \         /usr/lib ...

  10. python3.X和python2.7的区别

    1.python3.X将thread模块修改为_thread