ZOJ Problem Set - 2972 Hurdles of 110m

今天在网上看到一个讲动态规划的文章,是以01背包为例的,这文章和书上的讲解非常不一样,令我眼前一亮,于是转载一下下~~~
(说明一下,本人非常痛恨教材公式定理漫天飞,实际的讲解却讲得非常枯涩难懂,这种中国式的教育已经延绵了几千年了,现在中国的教材还是这个样子,讲清楚些明白些就那么难么?高中有个老师讲的一句话一直觉得很有道理:“教得会天才不是真本事,能把博士生的东西讲到小学生都会用那才是真水平。”)

附上原文地址:http://www.cnblogs.com/sdjl/articles/1274312.html

http://apps.hi.baidu.com/share/detail/17038766

浙大的这道题目也是可以用这个方法的。

In the year 2008, the 29th Olympic Games will be held in Beijing. This will signify the prosperity of China and Beijing Olympics is to be a festival for people all over the world as well.

Liu Xiang is one of the famous Olympic athletes in China. In 2002 Liu broke Renaldo Nehemiah's 24-year-old world junior record for the 110m hurdles. At the 2004 Athens Olympics Games, he won the gold medal in the end. Although he was not considered as a favorite for the gold, in the final, Liu's technique was nearly perfect as he barely touched the sixth hurdle and cleared all of the others cleanly. He powered to a victory of almost three meters. In doing so, he tied the 11-year-old world record of 12.91 seconds. Liu was the first Chinese man to win an Olympic gold medal in track and field. Only 21 years old at the time of his victory, Liu vowed to defend his title when the Olympics come to Beijing in 2008.

n the 110m hurdle competition, the track was divided into N parts by the hurdle. In each part, the player has to run in the same speed; otherwise he may hit the hurdle. In fact, there are 3 modes to choose in each part for an athlete -- Fast Mode, Normal Mode and Slow Mode. Fast Mode costs the player T1 time to pass the part. However, he cannot always use this mode in all parts, because he needs to consumeF1 force at the same time. If he doesn't have enough force, he cannot run in the part at the Fast Mode. Normal Mode costs the playerT2 time for the part. And at this mode, the player's force will remain unchanged. Slow Mode costs the playerT3 time to pass the part. Meanwhile, the player will earnF2 force as compensation. The maximal force of a player isM. If he already hasM force, he cannot earn any more force. At the beginning of the competition, the player has the maximal force.

The input of this problem is detail data for Liu Xiang. Your task is to help him to choose proper mode in each part to finish the competition in the shortest time.

Input

Standard input will contain multiple test cases. The first line of the input is a single integerT (1 <=T <= 50) which is the number of test cases. And it will be followed byT consecutive test cases.

Each test case begins with two positive integers N and M. And followingN lines denote the data for theN parts. Each line has five positive integersT1 T2 T3 F1 F2. All the integers in this problem are less than or equal to 110.

Output

Results should be directed to standard output. The output of each test case should be a single integer in one line, which is the shortest time that Liu Xiang can finish the competition.

Sample Input

2
1 10
1 2 3 10 10
4 10
1 2 3 10 10
1 10 10 10 10
1 1 2 10 10
1 10 10 10 10

Sample Output

1
6
我基于那个人的思路很快的就写出来了。我觉得他的这样的思路可以很快的写出题目。


#include <stdio.h>
int T1[1001];
int T2[1001];
int T3[1001];
int F1[1001];
int F2[1001];
int a[1001][1001];
int N;
int MV;
int min1(int i,int j)
{
    return i>j? j:i;
}
int f(int n,int V)
{
    if(n==N-1)
    {
        int t=T2[n];
        t=min1(T3[n],T2[n]);
        if(V>=F1[n])
          {
             t=min1(T1[n],t);    
          }
          return t;
    }else
    {
        int t1=-1,t2=0,t3=-1;
        if(V>=F1[n])
        {
            if(a[n+1][V-F1[n]]==-1)
                 a[n+1][V-F1[n]]=f(n+1,V-F1[n]);
             t1=a[n+1][V-F1[n]]+T1[n];
        }
        if(a[n+1][V]==-1)
            a[n+1][V]=f(n+1,V);
        t2=a[n+1][V]+T2[n];
        int temp=V+F2[n];
        if(temp>MV)
            temp=MV;
        if(a[n+1][temp]==-1)
            a[n+1][temp]=f(n+1,temp);
        t3=a[n+1][temp]+T3[n];
        int t=t2;
        if(t1!=-1)
        {
            t=min1(t1,t2);
        }
        t=min1(t,t3);
        return t;
    }
}
int main(int argc, char *argv[])
{
    int n,v;
    scanf("%d",&n);
    while(n--)
    {
        for(int i=0;i<1001;i++)
        {
            for(int j=0;j<1001;j++)
              a[i][j]=-1;
        }
        scanf("%d%d",&N,&v);
        MV=v;
        for(int i=0;i<N;i++)
        {
            scanf("%d%d%d%d%d",&T1[i],&T2[i],&T3[i],&F1[i],&F2[i]);
        }
        printf("%d\n",f(0,v));
    }
    return 0;
}


这是网络牛逼的人写的代码:

#include <stdio.h>

int min1(int i,int j)
{
    return i>j? j:i;
}
int main(int argc, char *argv[])
{
    int n,v,n1;
    int t1,t3,t2,t;
    int f1;
    int f2;
    int a[120][120];
    scanf("%d",&n);
    while(n--)
    {
        
        scanf("%d%d",&n1,&v);
        for(int i=0;i<=n1+1;i++)
        {
            for(int j=0;j<=v+1;j++)
              a[i][j]=0xffffff;
        }
        a[0][v]=0;
        for(int i=1;i<=n1;i++)
        {
            scanf("%d%d%d%d%d",&t1,&t2,&t3,&f1,&f2);
            for(int j=v;j>=0;j--)
            {
                a[i][j]=min1(a[i-1][j]+t2,a[i][j]);
                if(j-f1>=0)
                {
                    a[i][j-f1]=min1(a[i][j-f1],a[i-1][j]+t1);
                }
                int temp=j+f2;
                if(temp>=v)
                      temp=v;
                a[i][temp]=min1(a[i][temp],a[i-1][j]+t3);
            }
            
        }
        int min2=0xffffff;
        for(int i=0;i<=v;i++)
        {
            if(min2>a[n1][i])
               min2=a[n1][i];
        }
        printf("%d\n",min2);
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值