POJ6071 Lazy Running(最短路)
程序员文章站
2024-02-14 17:23:52
...
Lazy Running
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)Total Submission(s): 1301 Accepted Submission(s): 557
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
Recommend
liuyiding
题意:四个点依次排列,首尾相接。从第二个点出发,最后要求回到第二个点。问怎么走能使总路程为大于等于k的最小值。
非常巧妙的最短路。
考虑一个这样的情况,当我在某个时间回到第二个点时,此时总路程不够k,我是不是可以选择重复走第一个点和第二个点之间的路,或者第二个点和第三个点之间的路。
取d12和d23之间的较小值记为w,若存在一条长度为x的回路,那么必然存在一条长度为x+w的回路。把每一个点拆成0-w-1个点,求出同余最短路即可。
#include <map>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <queue>
#include <vector>
#include <ctime>
using namespace std;
typedef long long ll;
const int MAXN=240100;
const long long INF=0x7fffffffffffffff;
typedef long long int ll;
struct edge{
int v,next;
int w;
}e[MAXN<<1];
int tot=0;
int head[MAXN];
void add(int u,int v,int w){
e[tot].v=v;
e[tot].w=w;
e[tot].next=head[u];
head[u]=tot++;
}
long long K,W;
long long d[MAXN];
queue<int> que;
int vis[MAXN];
long long spfa(int s){
memset(vis,0,sizeof vis);
fill(d,d+MAXN,INF);
d[s]=0;
while(!que.empty())
que.pop();
que.push(s);
vis[s]=1;
while(que.size()){
int u=que.front();
vis[u]=0;
que.pop();
for(int k=head[u];k!=-1;k=e[k].next){
int v=e[k].v;
int w=e[k].w;
if(d[v]>d[u]+1LL*w){
d[v]=d[u]+1LL*w;
if(!vis[v]){
que.push(v);
vis[v]=1;
}
}
}
}
long long res=INF;
for(int i=W;i<2*W;i++){
long long temp=K-d[i];
//if(d[i]<INF){
// cout<<i<<" "<<d[i]<<" "<<endl;
// }
if(temp<=0)
res=min(res,d[i]);
else{
long long x=temp/W;
if(temp%W)
x++;
res=min(res,x*W+d[i]);
}
}
return res;
}
int main(){
int t;
scanf("%d",&t);
while(t--){
int d1,d2,d3,d4;
tot=0;
memset(head,-1,sizeof head);
scanf("%lld%d%d%d%d",&K,&d1,&d2,&d3,&d4);
W=min(d1,d2);
W*=2;
for(int i=0;i<W;i++){
add(i,(i+d1)%W+W,d1);
add(i+W,(i+d1)%W,d1);
add(i+W,(i+d2)%W+2*W,d2);
add(i+2*W,(i+d2)%W+W,d2);
add(i+2*W,(i+d3)%W+3*W,d3);
add(i+3*W,(i+d3)%W+2*W,d3);
add(i,(i+d4)%W+3*W,d4);
add(i+3*W,(i+d4)%W,d4);
}
printf("%lld\n",spfa(W));
}
}