【多校训练】hdu 6071 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  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.
 

题意:

给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 long
using 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值