HDU6071-Lazy Running
来源:互联网 发布:手机编辑epub软件 编辑:程序博客网 时间:2024/05/07 07:29
Lazy Running
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)Total Submission(s): 973 Accepted Submission(s): 398
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 are4 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 these4 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.
Checkpointp2 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
The system regards these
Checkpoint
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 are5 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
Output
For each test case, print a single line containing an integer, denoting the minimum distance.
Sample Input
12000 600 650 535 380
Sample Output
2165HintThe best path is 2-1-4-3-2.
Source
2017 Multi-University Training Contest - Team 4
题意:给出四个点1,2,3,4,1和2,2和3,3和4,4和1 之间有路相连,现在从2点出发,最后回到2点,要求路径大于等于k,问路径长度最短是多少
解题思路:取一条与2相连的权值最小的边w。若存在一条从起点到终点的长度为k的路径,那么必然存在一条长度为k+2w的路径,只要一开始在那条边上往返走就好了。设dis[i][j]表示从起点到i,路径长度模2w为j时,路径长度的最小值。
#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <algorithm>#include <map>#include <set>#include <stack>#include <queue>#include <vector>#include <bitset>#include <functional>using namespace std;#define LL long longconst LL INF = 0x3f3f3f3f3f3f3f3f;struct node{ int id; LL dis; bool operator <(const node &a)const { return dis>a.dis; }} pre,nt1;LL dis[5][60009],k,w[10],r;int s[5],nt[10],e[10],cnt;void add(int u,int v,LL val){ nt[cnt]=s[u],s[u]=cnt,e[cnt]=v,w[cnt++]=val; nt[cnt]=s[v],s[v]=cnt,e[cnt]=u,w[cnt++]=val;}void Dijkstra(){ memset(dis,INF,sizeof dis); dis[2][0] = 0; priority_queue<node>q; pre.id=2,pre.dis=0; q.push(pre); while(!q.empty()) { pre=q.top(); q.pop(); for(int i=s[pre.id]; ~i; i=nt[i]) { int ee=e[i]; if(pre.dis + w[i] >= dis[ee][(w[i]+pre.dis)%r]) continue; dis[ee][(w[i]+pre.dis)%r] = pre.dis+w[i]; nt1.id=ee; nt1.dis=pre.dis+w[i];; q.push(nt1); } }}int main(){ int t; scanf("%d",&t); while(t--) { memset(s,-1,sizeof s); cnt=0; int d1,d2,d3,d4; scanf("%lld%d%d%d%d",&k,&d1,&d2,&d3,&d4); r = 2 * min(d1,d2); add(1,2,d1); add(2,3,d2); add(3,4,d3); add(4,1,d4); Dijkstra(); LL ans = INF; for(int i=0; i<r; i++) { if(dis[2][i]>k) ans = min(dis[2][i],ans); else ans = min(ans,dis[2][i] + (k - dis[2][i])/r*r + ((k - dis[2][i])%r>0) * r); } printf("%lld\n",ans); }}
阅读全文
0 0
- [HDU6071] Lazy Running
- HDU6071-Lazy Running
- HDU6071 Lazy Running【最短路】
- HDU6071-Lazy Running 最短路+思维
- [HDU6071][2017多校第四场][Lazy Running]
- 2017多校训练Contest4: 1005 Lazy Running hdu6071
- hdu6071-最短路&思维&多校4&同余-Lazy Running
- hdu6071
- HDU 6071 Lazy Running
- HDU 6071 Lazy Running
- hdoj 6071 Lazy Running
- Lazy Running HDU
- HDU 6071 Lazy Running
- Lazy Running HDU
- Lazy Running(dijkstra+取模)
- Hdu-6071 Lazy Running(trick最短路)
- POJ6071 Lazy Running(最短路)
- HDU6070 Lazy Running (最短路)
- Android Activity往另外一个Activity传递数据,Fragment从另一个Activity获取数据
- Java关键字final、static使用总结
- Jenkins部署远程jboss
- Struts2项目实战 微云盘(二):项目结构
- Caffe 内存管理分析
- HDU6071-Lazy Running
- Raising Modulo Numbers 【poj-1995】【快速幂】
- java 程序初始化执行先后顺序
- 计算机网络常见面试题总结(二)
- 点击div以外的其他区域,div隐藏
- Unity3D研究院之Inspector面板枚举的别名与排序(八十九)
- 阿里云短信
- STM32F107——OTG模块之USB设备之虚拟串口移植(四)
- Xcode8 支持 iOS7及以下版本