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.
![](https://i-blog.csdnimg.cn/blog_migrate/cc241df0cce206f599d4dd431e0f8fee.jpeg)
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
2165HintThe best path is 2-1-4-3-2.
取w=min(d1,2,d2,3),那么对于每一种方案,均可以通过往返跑w这条边使得距离增加2w。也就是说,如果存在距离为k的方案,那么必然存在距离为k+2w的方案。
设disi,j表示从起点出发到达i,距离模2w为j时的最短路,那么根据dis2,j解不等式即可得到最优路线。
以上为官方题解
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cassert>
using namespace std;
const int N=61000;
const int oo=2147483647;
int g[5100000];
long long f[N];
bool ff[N];
int a[N],v[N];
int n;
long long an,m;
void work(int now,int p,int n)
{
//memset(f,255,sizeof(f));
for (int i=0;i<p;i++) f[i]=1ll*oo*oo,ff[i]=false;
f[now%p]=now;
int l=0;
int r=1;
g[1]=now%p;
while (l<r)
{
int x=g[++l];
ff[x]=false;
for (int i=1;i<=n;i++)
if (f[x]+v[i]<f[(x+v[i])%p])
{
f[(x+v[i])%p]=f[x]+v[i];
if (!ff[(x+v[i])%p])
{
ff[(x+v[i])%p]=true;
g[++r]=(x+v[i])%p;
}
}
}
for (int i=0;i<p;i++)
{
long long s=f[i];
if (f[i]<m)
{
s=f[i]+1ll*((m-f[i])/p+1)*p;
if ((m-f[i])%p==0) s-=p;
}
an=min(an,s);
}
}
int main()
{
int T;
scanf("%d",&T);
while (T--)
{
scanf("%lld",&m);
for (int i=1;i<=4;i++)
scanf("%d",&a[i]);
an=1ll*oo*oo;
for (int i=1;i<=4;i++)
v[i]=2*a[i];
work(a[1]+a[2]+a[3]+a[4],2*a[1],4);
work(2*a[1]+2*a[2],2*a[1],4);
work(2*a[2]+2*a[3],2*a[2],4);
work(2*a[1]+2*a[4],2*a[1],4);
v[1]=a[1]*2;v[2]=a[2]*2;v[3]=a[3]*2;
work(2*a[2],2*a[2],3);
v[1]=a[1]*2;v[2]=a[2]*2;v[3]=a[4]*2;
work(2*a[1],2*a[1],3);
v[1]=a[1]*2;v[2]=a[2]*2;
work(0,2*a[1],2);
printf("%lld\n",an);
}
}