hdu-6071 Lazy Running

Posted cautx

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hdu-6071 Lazy Running相关的知识,希望对你有一定的参考价值。

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

.
InputThe 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.OutputFor 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.

        
 

OJ-ID:
hdu-6071

author:
Caution_X

date of submission:
20191024

tags:
dp+dijkstra

description modelling:
有四个点1,2,3,4形成环1-2-3-4-1,四条边的权值表示距离,问能否找到一条路径从点2出发回到点2并且权值之和大于K

major steps to solve it:
w=min(d1,d2),其中d1是1,2之间的距离,d2是2,3之间的距离
假设有符合条件的最小权值和P,那么就会有最佳的P-w
设dp[i][j]:=从2出发到达i点时的最小权值和,其中j是权值和%(2*w),之所以%(2*w)是因为从2出发再回到2权值和最少2*w
同样是从2到达i点,不同的j表示不同的路径,若j相同但是从2到i的权值和不同,则选择权值和小的
(1)dijkstra更新dp[i][j]
(2)判断dp[2][j]是否大于K,若小于K则不足之处用w填充

AC code:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, int> P;
const ll INF = 1e18;
const int MAXN = 6e4 + 10;
vector<P> G[MAXN];
ll d[5][MAXN];
void dijkstra(ll w, int s) {
    for(int i = 1; i <= 4; i++) {
        fill(d[i], d[i] + w, INF);
    }
    priority_queue<P, vector<P>, greater<P> >q;
    q.push(P(0, s));
    d[s][0] = 0;
    while(!q.empty()) {
        P p = q.top(); q.pop();
        int v = p.second;
        if(p.first > d[v][p.first % w]) continue;
        for(int i = 0; i < (int)G[v].size(); i++) {
            P e = G[v][i];
            ll dist = e.first + d[v][p.first % w];
            if(dist < d[e.second][dist % w]) {
                d[e.second][dist % w] = dist;
                q.push(P(dist, e.second));
            }
        }
    }
}
int main() {
    int T;
    cin >> T;
    while(T--) {
        memset(G, 0, sizeof G);
        ll K, d1, d2, d3, d4;
        cin >> K >> d1 >> d2 >> d3 >> d4;
        G[1].push_back(P(d1, 2));
        G[2].push_back(P(d1, 1));
        G[2].push_back(P(d2, 3));
        G[3].push_back(P(d2, 2));
        G[3].push_back(P(d3, 4));
        G[4].push_back(P(d3, 3));
        G[4].push_back(P(d4, 1));
        G[1].push_back(P(d4, 4));
        ll w = 2 * min(d1, d2);
        dijkstra(w, 2);
        ll ans = INF;
        for(ll i = 0; i < w; i++) {
            if(d[2][i] >= K) {
                ans = min(ans, d[2][i]);
            } else {
                ll nd = K - d[2][i];
                ans = min(ans, d[2][i] + nd / w * w + (nd % w > 0) * w);
            }
        }
        cout << ans << endl;
    }
    return 0;
}

 

以上是关于hdu-6071 Lazy Running的主要内容,如果未能解决你的问题,请参考以下文章

HDU 6071 Lazy Running (同余最短路)

HDU 6071 Lazy Running(最短路)

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

HDU 6071 同余最短路 spfa

add application window with unknown token XXX Unable to add window;is your activity is running?(代码片段

add application window with unknown token XXX Unable to add window;is your activity is running?(代码片段