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 (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.
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.
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的剩余系的最短路径长度都被考虑了,所以答案必然是正确的。
类似题:cdoj1633http://acm.uestc.edu.cn/#/problem/show/1633
1 #include <bits/stdc++.h> 2 3 using namespace std; 4 5 #define MP make_pair 6 #define PB push_back 7 typedef long long LL; 8 typedef pair<int,int> PII; 9 const double eps=1e-8; 10 const double pi=acos(-1.0); 11 const int K=1e6+7; 12 const int mod=1e9+7; 13 14 int n,d12,d23,d34,d41;; 15 LL d[5][100004],dk,p; 16 vector<PII>mp[5]; 17 void spfa(void) 18 { 19 queue<pair<LL,LL>>q; 20 for(int i=1;i<=4;i++) 21 for(int j=0;j<p;j++) 22 d[i][j]=2e18; 23 q.push(MP(2LL,0LL)); 24 while(q.size()) 25 { 26 LL u=q.front().first,w=q.front().second; 27 q.pop(); 28 if(w>dk*2)continue; 29 for(auto v:mp[u]) 30 if(d[v.first][(w+v.second)%p]>w+v.second) 31 { 32 d[v.first][(w+v.second)%p]=w+v.second; 33 q.push(MP(v.first,w+v.second)); 34 } 35 } 36 } 37 int main(void) 38 { 39 int t;cin>>t; 40 while(t--) 41 { 42 memset(mp,0,sizeof mp); 43 scanf("%lld%d%d%d%d",&dk,&d12,&d23,&d34,&d41); 44 mp[1].PB(MP(2,d12)),mp[1].PB(MP(4,d41)); 45 mp[2].PB(MP(1,d12)),mp[2].PB(MP(3,d23)); 46 mp[3].PB(MP(2,d23)),mp[3].PB(MP(4,d34)); 47 mp[4].PB(MP(3,d34)),mp[4].PB(MP(1,d41)); 48 p=2LL*min(d12,d23); 49 spfa(); 50 LL ans=2e18; 51 for(int i=0;i<p;i++) 52 if(d[2][i]>=dk) 53 ans=min(d[2][i],ans); 54 else 55 ans=min((dk-d[2][i]+p-1)/p*p+d[2][i],ans); 56 printf("%lld\n",ans); 57 } 58 return 0; 59 }
作者:weeping
出处:www.cnblogs.com/weeping/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。