hdu 2059 龟兔赛跑

DP。纠结好久了都。

最终还是有点考虑错了。我想的是按位置DP,状态是在加油站 加油,或者不加油。然后取之前状态的时间最小值。

但是我考虑的一直是上个状态,这个有问题。应该考虑之前所有状态的。这样的话,取之前所有状态加油,或者不加油的最小值。

用二维DP存,存的是当前节点还能骑多少距离,以及已经骑了多长时间。

过了后看别人代码,很简短,不过还是我看我的代码思路比较清晰。。。本来想的都不一样,再转过头去想别人的,真的好难理解啊。


#include <queue>
#include <stack>
#include <math.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <limits.h>
#include <string.h>
#include <string>
#include <algorithm>
#define MID(x,y) ( ( x + y ) >> 1 )
#define L(x) ( x << 1 )
#define R(x) ( x << 1 | 1 )
#define BUG puts("here!!!")

using namespace std;

const int MAX = 110;
struct NODE{ int ll; double t;};
NODE a[2][MAX];
int len[MAX];

int main()
{
    int v1,v2,vr,l,n,c,t;
    
    while( ~scanf("%d",&l) )
    {
        scanf("%d%d%d%d%d%d",&n, &c, &t, &vr, &v1, &v2);
        
        memset(a, 0, sizeof(a));
        for(int i=1; i<=n; i++)
            scanf("%d",&len[i]);
        
        if( v2 >= v1 )        //蹬的速度比骑的快 。。。 
        {
            double t1 = l * 1.0 / v2;
            double t2 = l * 1.0 / vr;
            if( t1 < t2 )
                printf("What a pity rabbit!\n");
            else
                printf("Good job,rabbit!\n");
            continue;
        }
        
        len[0] = 0; len[n+1] = l;
        a[0][0].ll = c; a[0][0].t = 0;
        a[1][0].ll = c; a[1][0].t = 0;
        
        n++;
        for(int i=1; i<=n; i++)
        {
            double t1, t2;
            a[0][i].t = a[1][i].t = INT_MAX;
            for(int k=0; k<i; k++)
            {
                int delta = len[i] - len[k];
            
                if( delta <= a[0][k].ll )			//计算时间t1 
                    t1 = delta * 1.0 / v1;
                else
                    t1 = a[0][k].ll * 1.0 / v1 + (delta - a[0][k].ll) * 1.0 / v2;
                
                if( delta <= a[1][k].ll )			//计算时间t2 
                    t2 = delta * 1.0 / v1;
                else
                    t2 = a[1][k].ll * 1.0 / v1 + (delta - a[1][k].ll) * 1.0 / v2;    
            
                double tt1 = t1 + a[0][k].t;
                double tt2 = t2 + a[1][k].t;
                
                a[0][i].t = min(a[0][i].t, min(tt1, tt2));	// 取最小值 
            
                if( a[0][i].t == tt1 )				// 更新当前状态 
                    if( delta <= a[0][k].ll )
                        a[0][i].ll = a[0][k].ll - delta;
                    else
                        a[0][i].ll = 0;
            
                if( a[0][i].t == tt2 )
                    if( delta <= a[1][k].ll )
                        a[0][i].ll = a[1][k].ll - delta;
                    else
                        a[0][i].ll = 0;
            
                a[1][i].t = min(a[1][i].t, min(tt1, tt2) + t );
                a[1][i].ll = c;
            }
        }

        double ans = min(a[0][n].t, a[1][n].t - t);
        double rt = l * 1.0 / vr;

        if( ans < rt )
            printf("What a pity rabbit!\n");
        else
            printf("Good job,rabbit!\n");
    }

return 0;
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值