【多校训练】hdu 6071 Lazy Running 最短路径 spfa
来源:互联网 发布:查理沃德 知乎 编辑:程序博客网 时间:2024/05/18 03:55
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.
题意:
给4个节点组成的环,求从点2出发,回到点2距离不小于k的最短路
思路:
这个回路肯定是由一个或者多个环组成的。一个环可以分成两个环x+ny的形式,n是y环的个数。而需要求解的环也可以分成这样两个环。
举例说,我们需要求解一个不小于1000的环,我们走2->1->2可以得到大小为30的环。如果我们能找到另一个环,大小为10,就能得到30*33+10=1000的环。但是这样的环不一定存在,如果存在大小为50的环,我们也可以得到30*32+50=1020的环。因为50>30,其实存在大小为20,50,80...的环都能得到的到1020的环。总而言之,我们只需要找是否存在环的大小%30等于0~29的环即可。
枚举量和y环的大小相关,所以我们要找最小的环。
具体看代码:
//// main.cpp// 1005//// Created by zc on 2017/8/25.// Copyright © 2017年 zc. All rights reserved.//#include <iostream>#include<cstdio>#include<cstring>#include<cmath>#include<queue>#include<algorithm>#define ll long longusing namespace std;const int N= 66000;ll k,mp[5][5],ans,m,d[5][N];int v[5][N];void spfa(){ memset(d,0x3f,sizeof(d)); memset(v,0,sizeof(v)); queue<pair<int,ll> >q; d[2][0]=0; v[2][0]=1; q.push(make_pair(2,0)); while(!q.empty()) { auto t=q.front(); q.pop(); int u=t.first; ll c=t.second; v[u][c%m]=1; if(u==2) { if(c>k) ans=min(ans,c); else ans=min(ans,c+((k-c-1)/m+1)*m); } for(int i=-1;i<2;i+=2) { int p=(u+i+3)%4+1; ll w=c+mp[u][p]; if(d[p][w%m]>w) { d[p][w%m]=w; if(v[p][w%m]==0) { v[p][w%m]=1; q.push(make_pair(p,w)); } } } }}int main(int argc, const char * argv[]) { int T; scanf("%d",&T); while(T--) { scanf("%lld",&k); for(int i=1;i<=4;i++) { int j=(i%4)+1; scanf("%lld",&mp[i][j]); mp[j][i]=mp[i][j]; } m=2*min(mp[2][1],mp[2][3]); ans=((k-1)/m+1)*m; spfa(); printf("%lld\n",ans); }}
阅读全文
0 0
- 【多校训练】hdu 6071 Lazy Running 最短路径 spfa
- 【多校训练】hdu 6166 Senior Pan 最短路径 Dijkstra
- hdu 6071 Lazy Running(spfa+同余最短路)
- 2017多校训练Contest4: 1005 Lazy Running hdu6071
- 【多校训练】hdu 6181 Two Paths 次短路径 Dijkstra
- HDU 3790 最短路径问题 (SPFA)
- HDU 3790 最短路径问题 (SPFA)
- hdu 3790 最短路径问题 spfa
- hdu 3790 最短路径问题 spfa
- HDU 3790 最短路径(spfa)
- Hdu-6071 Lazy Running(trick最短路)
- spfa最短路径
- 最短路径spfa
- spfa 最短路径
- 最短路径 -- spfa
- SPFA最短路径
- SPFA--最短路径
- HDU 6071 Lazy Running
- QT学习笔记—.pro变量解析
- 程序员读书之三毛传
- 访问图像像素信息方式的优化
- JavaScript通过style只能获取内联 状态的样式
- 集训第二十六天(2017/8/25):集训总结
- 【多校训练】hdu 6071 Lazy Running 最短路径 spfa
- python零碎知识(3)--字典
- Spring的导入jar包报错
- 暑假ACM集训报告总结
- Android通信一:Socket通信
- Spring框架基本,使用maven
- apple6s No service
- 两小时学会Git玩转Github-资料 零散笔记(2017年8月25日19:14:51)
- 定位