hdu 2059 龟兔赛跑(DP)

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/fly_tzf/article/details/72824305

思路:用dp[i][0]表示到第i个加油站不加油所用的最小时间,dp[i][1]表示到第i个加油站加油所用的最小时间。将终点设为第n+1个站,则答案为dp[n+1][0].具体细节看代码。

#include<cstdio>
#include<cmath>
#include<cstring>
#include<iostream>
using namespace std;
typedef long long LL;
const LL INF = 100000000000;
const double EPS = 1e-9;
int a[110];
double dp[110][2];
int dcmp(double x, double y){
    if(fabs(x - y) < EPS) return 0;
    return x > y ? 1 : -1;
}
int main(){
    int l, n, c, t;
    int vr, vt1, vt2;
    while(scanf("%d", &l) == 1){
        scanf("%d%d%d%d%d%d", &n, &c, &t, &vr, &vt1, &vt2);
        for(int i = 1; i <= n; ++i)
            scanf("%d", &a[i]);
        memset(dp, 0, sizeof dp);
        a[0] = 0, a[n + 1] = l;
        for(int i = 1; i <= n + 1; ++i){
            dp[i][0] = dp[i][1] = INF;
            for(int j = 0; j < i; ++j){
                int d = a[i] - a[j];
                    dp[i][0] = min(dp[i][0], dp[j][0] + (double)d / vt2);
                    if(d > c)
                        dp[i][0] = min(dp[i][0], dp[j][1] + (double)c / vt1 + (double)(d - c) / vt2);
                    else
                        dp[i][0] = min(dp[i][0], dp[j][1] + (double)d / vt1);
                    dp[i][1] = min(dp[i][1], dp[j][0] + (double)d / vt2 + t);
                    if(d > c)
                        dp[i][1] = min(dp[i][1], dp[j][1] + (double)c / vt1 + (double)(d - c) / vt2 + t);
                    else
                        dp[i][1] = min(dp[i][1], dp[j][1] + (double)d / vt1 + t);
            }
        }
        double t1 = (double)l / vr, t2;
        if(t1 > dp[n + 1][0]) printf("What a pity rabbit!\n");
        else printf("Good job,rabbit!\n");
    }
}


展开阅读全文

没有更多推荐了,返回首页