hdu 6017 Lazy Running —— 同余+spfa

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.

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.

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.

这道题一开始也不会做,去看了别人的博客才会的,说是用同余,但是讲的很难理解,我是这样想的,因为它的k>=1,所以至少需要经过两次2旁边的最短路,那么就赋初值为所有走过的路都是走这条路,然后同余是怎么个意思呢,就是说
(n*mod-x)%mod=0;如果x=1,那么当x=mod*2+1的时候还是1,也就是最短的那两条路多走了两遍,所以是等价的,那么之后就是下一步走到2的时候找个最小值就好了。

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const ll inf=1e18+5;
ll val[5][5],dist[5][60005],k,mod;
ll ans;
int inq[5][60005];
int mov[]={-1,1};
struct node
{
    int pos;
    ll dis;
    node(){}
    node(int pos,ll dis):pos(pos),dis(dis){}
};
void spfa()
{
    dist[1][0]=0;
    inq[1][0]=1;
    queue<node>Q;
    Q.push(node(1,0));
    while(!Q.empty())
    {
        node v=Q.front();
        Q.pop();
        inq[v.pos][v.dis%mod]=0;
        for(int i=0;i<=1;i++)
        {
            int ne=(v.pos+4+mov[i])%4;
            ll dis=v.dis+val[v.pos][ne];
            ll nedis=dis%mod;
            if(ne==1)
            {
                if(dis<k)
                    ans=min(ans,dis+((k-dis-1)/mod+1)*mod);
                else
                    ans=min(ans,dis);
            }
            if(dist[ne][nedis]>dis)
            {
                dist[ne][nedis]=dis;
                if(!inq[ne][nedis])
                {
                    Q.push(node(ne,dis));
                    inq[ne][nedis]=1;
                }
            }
        }
    }
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        for(int i=0;i<5;i++)
            for(int j=0;j<60005;j++)
                dist[i][j]=inf,inq[i][j]=0;
        ll d1,d2,d3,d4;
        scanf("%lld%lld%lld%lld%lld",&k,&d1,&d2,&d3,&d4);
        val[0][1]=val[1][0]=d1;
        val[1][2]=val[2][1]=d2;
        val[2][3]=val[3][2]=d3;
        val[3][0]=val[0][3]=d4;
        mod=2*min(d1,d2);
        ans=(ll)((k-1)/mod+1)*mod;
        spfa();
        printf("%lld\n",ans);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值