HDU6071-Lazy Running

本文介绍了一种解决特定跑步路径问题的算法。问题要求从指定起点开始并返回该点,路径总长度不得低于一定数值。通过使用图论和动态规划的方法,找到满足条件的最短跑步路径。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Lazy Running

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


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
 

题意:给出四个点1,2,3,4,1和2,2和3,3和4,4和1 之间有路相连,现在从2点出发,最后回到2点,要求路径大于等于k,问路径长度最短是多少

解题思路:取一条与2相连的权值最小的边w。若存在一条从起点到终点的长度为k的路径,那么必然存在一条长度为k+2w的路径,只要一开始在那条边上往返走就好了。设dis[i][j]表示从起点到i,路径长度模2w为j时,路径长度的最小值。


#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <functional>

using namespace std;

#define LL long long
const LL INF = 0x3f3f3f3f3f3f3f3f;

struct node
{
    int id;
    LL dis;
    bool operator <(const node &a)const
    {
        return dis>a.dis;
    }
} pre,nt1;
LL dis[5][60009],k,w[10],r;
int s[5],nt[10],e[10],cnt;

void add(int u,int v,LL val)
{
    nt[cnt]=s[u],s[u]=cnt,e[cnt]=v,w[cnt++]=val;
    nt[cnt]=s[v],s[v]=cnt,e[cnt]=u,w[cnt++]=val;
}

void Dijkstra()
{
    memset(dis,INF,sizeof dis);
    dis[2][0] = 0;
    priority_queue<node>q;
    pre.id=2,pre.dis=0;
    q.push(pre);
    while(!q.empty())
    {
        pre=q.top();
        q.pop();
        for(int i=s[pre.id]; ~i; i=nt[i])
        {
            int ee=e[i];
            if(pre.dis + w[i] >= dis[ee][(w[i]+pre.dis)%r]) continue;
            dis[ee][(w[i]+pre.dis)%r] = pre.dis+w[i];
            nt1.id=ee;
            nt1.dis=pre.dis+w[i];;
            q.push(nt1);
        }
    }
}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        memset(s,-1,sizeof s);
        cnt=0;
        int d1,d2,d3,d4;
        scanf("%lld%d%d%d%d",&k,&d1,&d2,&d3,&d4);
        r = 2 * min(d1,d2);
        add(1,2,d1);
        add(2,3,d2);
        add(3,4,d3);
        add(4,1,d4);
        Dijkstra();
        LL ans = INF;
        for(int i=0; i<r; i++)
        {
            if(dis[2][i]>k) ans = min(dis[2][i],ans);
            else ans = min(ans,dis[2][i] + (k - dis[2][i])/r*r + ((k - dis[2][i])%r>0) * r);
        }
        printf("%lld\n",ans);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值