HDU 6071 Lazy Running (构造同余最短路+思维好题)

Lazy Running

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 1598    Accepted Submission(s): 677


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.
 

Source
 

Recommend

liuyiding   |   We have carefully selected several similar problems for you:   6297  6296  6295  6294  6293 

#include<set>//int gcd(int a,int b){return b?gcd(b,a%b):a;}
#include<vector>
#include<cmath>
#include<stack>
#include<queue>
#include<string.h>
#include<stdlib.h>
#include<cstdio>
#define LL  long  long
#define mem(a,b) memset(a,b,sizeof(a));
#define inf 0x3f3f3f3f
using namespace std;

const int maxn=60010;
LL dist[5][maxn],G[5][5];
LL ans,k;
bool vis[5][maxn];
/*
题意:给定一个环和四条边代表权重,
再给定一个k代表上限,问如何从2出发再回到2经过的路径和超过k且最短

用spfa算法来遍历从2出发到每个点的同余最短路,
注意这里标记走没走过不是以往的看经没经过,
而是看走到这里的权重和的余数,四个点均是如此。

每次经过原点时更新答案,用一个小技巧就是求不小于等于的技巧。
同余的话模的取法,便是整体思路精髓所在,
因为如果有一个答案为s的路径环,那么s+2*d*n这个集合均是大于k的满足条件的环。
其中d是到1和到3中最短的距离。
所以如果存在两个关于2*d同余的答案,那么肯定有一个答案可以不予考虑。

*/
struct node
{
    int u;
    LL dis;
    node(int x=0,LL y=0)
    {
        u=x;
        dis=y;
    }
};
int m;
void spfa(int s)
{
    mem(vis,false);
    mem(dist,inf);
    queue<node> seq;
    dist[s][0]=0;
    vis[s][0]=true;
    seq.push(node(s,0));
    while(!seq.empty())
    {
        int uu=seq.front().u;
        LL now_dis=seq.front().dis;
        seq.pop();
        for(int i=-1;i<2;i+=2)
        {
            int vv=(uu+i+4)%4;
            LL next_dis=now_dis+G[uu][vv],next_p=next_dis%m;
            if(vv==s)//如果回到原点
            {
                if(next_dis<k) ans=min(ans,((k-1-next_dis)/m+1)*m+next_dis);//出现一个可选答案,进行比较
                else ans=min(next_dis,ans);
            }
            if(dist[vv][next_p]>next_dis)
            {
                dist[vv][next_p]=next_dis;
                if(vis[vv][next_p]) continue;
                vis[vv][next_p]=true;
                seq.push(node(vv,next_dis));
            }
        }
    }
}


int main()
{
    int t;scanf("%d",&t);
    while(t--)
    {
        scanf("%lld",&k);
        mem(G,0);
        for(int i=0;i<4;i++)
        {
            int j=(i+1)%4;
            scanf("%d",&G[i][j]);
            G[j][i]=G[i][j];
        }
        m=min(G[1][0],G[1][2])*2;
        ans=((k-1)/m+1)*m;//不小于等于的经典做法
        spfa(1);
        printf("%lld\n",ans);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值