hdu 2059 龟兔赛跑(DP)

思路:用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");
    }
}


相关推荐
<p> <strong><span style="font-size:20px;color:#FF0000;">本课程主要针对计算机相关专业的正在做毕设的学生与需要项目实战练习的Java学习者</span></strong> </p> <p> <span style="color:#FF0000;"><strong><span style="font-size:18px;">1. 包含:<span style="color:#FFFF00;background-color:#FF0000;">项目源码、</span><span style="color:#FFFF00;background-color:#FF0000;">项目文档、数据库脚本、软件工具</span>等所有资料</span></strong></span> </p> <p> <span style="color:#FF0000;"><strong><span style="font-size:18px;">2. 手把手的带你从零开始部署运行本套系统</span></strong></span> </p> <p> <span style="color:#FF0000;"><strong><span style="font-size:18px;">3. 该项目附带的源码资料可作为毕设使用</span></strong></span> </p> <p> <span style="color:#FF0000;"><strong><span style="font-size:18px;">4. 提供技术答疑和远程协助指导</span></strong></span><strong><span style="font-size:18px;"></span></strong> </p> <p> <br /> </p> <p> <span style="font-size:18px;"><strong>项目运行截图:</strong></span> </p> <p> <strong><span style="font-size:18px;">1系统登陆界面</span></strong> </p> <p> <strong><span style="font-size:18px;"><img src="https://img-bss.csdn.net/202002241015433522.png" alt="" /><br /> </span></strong> </p> <p> <strong><span style="font-size:18px;"><strong><span style="font-size:18px;">2学生模块</span></strong></span></strong> </p> <p> <strong><span style="font-size:18px;"><img src="https://img-bss.csdn.net/202002241015575966.png" alt="" /></span></strong> </p> <p> <strong><span style="font-size:18px;"><strong><span style="font-size:18px;">3教师模块</span></strong></span></strong> </p> <p> <strong><span style="font-size:18px;"><img src="https://img-bss.csdn.net/202002241016127898.png" alt="" /></span></strong> </p> <p> <strong><span style="font-size:18px;"><strong><span style="font-size:18px;">4系统管理员</span></strong></span></strong> </p> <p> <strong><span style="font-size:18px;"><img src="https://img-bss.csdn.net/202002241016281177.png" alt="" /></span></strong> </p> <p> <strong><span style="font-size:18px;"><img src="https://img-bss.csdn.net/202002241016369884.png" alt="" /></span></strong> </p> <p> <strong><span style="font-size:18px;"><br /> </span></strong> </p> <p> <strong><span style="font-size:18px;"><strong><span style="font-size:18px;">更多Java毕设项目请关注我的毕设系列课程 <a href="https://edu.csdn.net/lecturer/2104">https://edu.csdn.net/lecturer/2104</a></span></strong></span></strong> </p> <p> <strong><span style="font-size:18px;"><br /> </span></strong> </p>
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页