HDU 6071 Lazy Running (同余最短路 dij)

Lazy Running

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

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.

Source

2017 Multi-University Training Contest - Team 4

【题意】四个点连成环,相邻两个点之间有距离,问从点 1 出发回到点1 ,总距离超过K 的最短路是多少。

【分析】像这种无限走下去的题,可以用同余最短路来解。点1相邻的两条边,设最短的那条长度为,m,那么存在一条长度为x的回到1节点的路,就一定存在长度为x+2*m的路。

dis[i][j]表示到达i点总长度%2*m==j的最短路,然后dij就行了。

#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
#define met(a,b) memset(a,b,sizeof a)
#define pb push_back
#define mp make_pair
#define rep(i,l,r) for(int i=(l);i<=(r);++i)
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int N = 6e4+50;;
const int M = 255;
const int mod = 19260817;
const int mo=123;
const double pi= acos(-1.0);
typedef pair<int,int>pii;
typedef pair<ll,int>P;
int n,s;
ll dis[6][N];
ll k,edg[6][6],m,ans;
void dij(int s){
    priority_queue<P,vector<P>,greater<P> >q;
    for(int i=0;i<4;i++){
        for(int j=0;j<=m;j++){
            dis[i][j]=1e18;
        }
    }
    q.push(P(0LL,s));
    while(!q.empty()){
        ll w=q.top().first;
        int u=q.top().second;
        q.pop();
        if(u==s){
            if(w<k){
                ans=min(ans,w+((k-w-1)/m+1)*m);
            }
            else ans=min(ans,w);
        }
        for(int i=0;i<4;i++){
            if(!edg[u][i])continue;
            ll d=w+edg[u][i];
            if(dis[i][d%m]>d){
                dis[i][d%m]=d;
                q.push(P(d,i));
            }
        }
    }
}
int main(){
    int T;
    scanf("%d",&T);
    while(T--){
        ans=1e18;
        scanf("%lld",&k);
        for(int i=0;i<4;i++){
            scanf("%lld",&edg[i][(i+1)%4]);
            edg[(i+1)%4][i]=edg[i][(i+1)%4];
        }
        m=2*min(edg[1][0],edg[1][2]);
        ans=((k-1)/m+1)*m;
        dij(1);
        printf("%lld\n",ans);
    }
    return 0;
}
时间: 2024-10-12 04:10:57

HDU 6071 Lazy Running (同余最短路 dij)的相关文章

hdu 6071 Lazy Running(同余最短路)

题目链接:hdu 6071 Lazy Running 题意: 给你4个点,每两个相邻点有一个距离,现在让你在这四个点来回跑步,从2开始,最后回到2,问你找一个距离ans,ans>=k,问最小的ans是多少. 题解: Claris的官方题解: 1 #include<bits/stdc++.h> 2 #define F(i,a,b) for(int i=(a);i<=(b);++i) 3 using namespace std; 4 using ll=long long; 5 6 co

HDU 6071 Lazy Running (同余最短路)

Lazy Running Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)Total Submission(s): 101    Accepted Submission(s): 40 Problem Description In HDU, you have to run along the campus for 24 times, or you will fail in PE

HDU 6071 Lazy Running(最短路)

[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=6071 [题目大意] 给出四个点1,2,3,4,1和2,2和3,3和4,4和1 之间有路相连, 现在从2点出发,最后回到2点,要求路径大于等于K,问路径长度最短是多少 [题解] 取一条与2相连的权值最小的边w. 若存在一条从起点到终点的长度为k的路径, 那么必然存在一条长度为k+2w的路径,只要一开始在那条边上往返走就好了. 设dij表示从起点到i,路径长度模2w为j时,路径长度的最小值. 用最短

HDU 6071 同余最短路 spfa

Lazy Running Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)Total Submission(s): 657    Accepted Submission(s): 284 Problem Description In HDU, you have to run along the campus for 24 times, or you will fail in P

【66测试20161115】【树】【DP_LIS】【SPFA】【同余最短路】【递推】【矩阵快速幂】

还有3天,今天考试又崩了.状态还没有调整过来... 第一题:小L的二叉树 勤奋又善于思考的小L接触了信息学竞赛,开始的学习十分顺利.但是,小L对数据结构的掌握实在十分渣渣.所以,小L当时卡在了二叉树. 在计算机科学中,二叉树是每个结点最多有两个子结点的有序树.通常子结点被称作“左孩子”和“右孩子”.二叉树被用作二叉搜索树和二叉堆.随后他又和他人讨论起了二叉搜索树.什么是二叉搜索树呢?二叉搜索树首先是一棵二叉树.设key[p]表示结点p上的数值.对于其中的每个结点p,若其存在左孩子lch,则key

HDU 1596 find the safest road (最短路)

find the safest road Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 6973    Accepted Submission(s): 2469 Problem Description XX星球有很多城市,每个城市之间有一条或多条飞行通道,但是并不是所有的路都是很安全的,每一条路有一个安全系数s,s是在 0 和 1

HDU 4063 线段与圆相交+最短路

Aircraft Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 980    Accepted Submission(s): 228 Problem Description You are playing a flying game. In the game, player controls an aircraft in a 2D-

HDU 2066-一个人的旅行(最短路Dijkstra)

一个人的旅行 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 19349    Accepted Submission(s): 6763 Problem Description 虽然草儿是个路痴(就是在杭电待了一年多,居然还会在校园里迷路的人,汗~),但是草儿仍然很喜欢旅行,因为在旅途中 会遇见很多人(白马王子,^0^),很多事,还能丰

hdu 4885 TIANKENG’s travel (最短路+判断三点共线)

TIANKENG's travel Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 408    Accepted Submission(s): 100 Problem Description TIANKENG has get a driving license and one day he is so lucky to find a