地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=6071

题目:

Lazy Running

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

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
 
思路:
  一道做过类似题的题目,在比赛时想了半个小时都没想出来,然后就做别的题去了。
  做过的题还不会做,好气啊
  记w为2和相邻节点路径的最小值,则如果存在一条从2出发,从2结束的路径(长度为x),那么x+w*2也必然是合法的。(这里的w取相邻节点路径的最小值是为了优化时间,不取最小值也可以)
  所以d[i][x]记为到达i节点且路径长度模2*w为x的最短路径长度。求出d[2][x]数组后就可以通过枚举x得到答案。
  那为什么这样做是对的呢?
  因为到达2的所有模2*w的剩余系的最短路径长度都被考虑了,所以答案必然是正确的。
 #include <bits/stdc++.h>

 using namespace std;

 #define MP make_pair
#define PB push_back
typedef long long LL;
typedef pair<int,int> PII;
const double eps=1e-;
const double pi=acos(-1.0);
const int K=1e6+;
const int mod=1e9+; int n,d12,d23,d34,d41;;
LL d[][],dk,p;
vector<PII>mp[];
void spfa(void)
{
queue<pair<LL,LL>>q;
for(int i=;i<=;i++)
for(int j=;j<p;j++)
d[i][j]=2e18;
q.push(MP(2LL,0LL));
while(q.size())
{
LL u=q.front().first,w=q.front().second;
q.pop();
if(w>dk*)continue;
for(auto v:mp[u])
if(d[v.first][(w+v.second)%p]>w+v.second)
{
d[v.first][(w+v.second)%p]=w+v.second;
q.push(MP(v.first,w+v.second));
}
}
}
int main(void)
{
int t;cin>>t;
while(t--)
{
memset(mp,,sizeof mp);
scanf("%lld%d%d%d%d",&dk,&d12,&d23,&d34,&d41);
mp[].PB(MP(,d12)),mp[].PB(MP(,d41));
mp[].PB(MP(,d12)),mp[].PB(MP(,d23));
mp[].PB(MP(,d23)),mp[].PB(MP(,d34));
mp[].PB(MP(,d34)),mp[].PB(MP(,d41));
p=2LL*min(d12,d23);
spfa();
LL ans=2e18;
for(int i=;i<p;i++)
if(d[][i]>=dk)
ans=min(d[][i],ans);
else
ans=min((dk-d[][i]+p-)/p*p+d[][i],ans);
printf("%lld\n",ans);
}
return ;
}

最新文章

  1. 关于Function.prototype.bind
  2. 第二章 rabbitmq在mac上的安装
  3. ccpc-1008-HDU5839Special Tetrahedron-计算几何
  4. 正则匹配中文.PHP不兼容的问题
  5. [HDU 4585] Shaolin (map应用)
  6. 锋利的jquery-选择器
  7. 对Kalman(卡尔曼)滤波器的理解
  8. bootstropt-table 大量字段整体表单上传之时间处理
  9. IIS下自定义错误页面配置的两种方式(亲测可行)--IIS服务器
  10. Exp2 后门原理与实践 20164311
  11. Codeforces 830C Bamboo Partition 其他
  12. noip2016海港
  13. Linux命令:pwd
  14. 五花八门的CSS
  15. Linux 下载安装配置Redis完整步骤
  16. Go Revel - app.conf
  17. hdu2243 考研路茫茫——单词情结【AC自动机】【矩阵快速幂】
  18. pm2以windows服务运行
  19. ubuntu64位库
  20. Oracle 与Sql Server常用函数对比

热门文章

  1. 利用新浪云平台(SAE) 搭建 HUSTOJ 简易教程
  2. ios 从URL中截取所包含的参数,并且以字典的形式返回和参数字典转URL
  3. (转)有关thread线程
  4. MyEclipse------如何添加jspsmartupload.jar,用于文件上传
  5. Codeforces Round #296 (Div. 2) B. Error Correct System
  6. Makefile foreach(转)
  7. Myeclipse下使用Maven搭建spring boot项目
  8. 【BZOJ2956】模积和 分块
  9. 该死的Kafka,远程连接Kafka超时以及解决办法
  10. Java 使用阿里云短信的API接口