2017多校训练Contest4: 1005 Lazy Running hdu6071

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  pi1  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(1T15) , denoting the number of test cases.

In each test case, there are  5  integers  K,d1,2,d2,3,d3,4,d4,1(1K1018,1d30000) , 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.


w=\min(d_{1,2},d_{2,3})w=min(d1,2,d2,3),那么对于每一种方案,均可以通过往返跑ww这条边使得距离增加2w2w。也就是说,如果存在距离为kk的方案,那么必然存在距离为k+2wk+2w的方案。

dis_{i,j}disi,j表示从起点出发到达ii,距离模2w2wjj时的最短路,那么根据dis_{2,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);
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值