H - Hurdles of 110m----(2015 summer training #4 (Qualifying))

来源:互联网 发布:淘宝店铺口令怎么生成 编辑:程序博客网 时间:2024/05/17 23:31

H - Hurdles of 110m
时限:2000MS     内存:65536KB     64位IO格式:%lld & %llu

问题描述

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.

In 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 consume F1 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 player T2 time for the part. And at this mode, the player's force will remain unchanged. Slow Mode costs the player T3 time to pass the part. Meanwhile, the player will earn F2 force as compensation. The maximal force of a player is M. If he already has M 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.


输入

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

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


输出

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.


样例输入

21 101 2 3 10 104 101 2 3 10 101 10 10 10 101 1 2 10 101 10 10 10 10


样例输出

16

Hint

For the second sample test case, Liu Xiang should run with the sequence of Normal Mode, Fast Mode, Slow Mode and Fast Mode.


分析:比赛时,用dfs写了代码,提交果断TLE。船长给的思路是动态规划,想了很久也没想出来咋写(PS:DP有点弱,背包问题忘了又忘),后来看了网上的题解,总算AC了,我要恶补DP,come on!


CODE:(DFS)

#include <iostream>#include <cstdio>using namespace std;const int maxn=1005;int ans,tim;int n,m,t1[maxn],t2[maxn],t3[maxn],f1[maxn],f2[maxn];void dfs(int sum,int i){    if(i==n){        if(tim<ans)            ans=tim;        return ;    }    if(sum>=f1[i]){        tim+=t1[i];        dfs(sum-f1[i],i+1);        tim-=t1[i];        tim+=t2[i];        dfs(sum,i+1);        tim-=t2[i];    }    else{        tim+=t3[i];        dfs(sum+f2[i],i+1);        tim-=t3[i];    }    return ;}int main(){    int T;    cin>>T;    while(T--){        ans=100000,tim=0;        cin>>n>>m;        for(int i=0;i<n;i++){//            cin>>t1[i]>>t2[i]>>t3[i]>>f1[i]>>f2[i];            scanf("%d%d%d%d%d",&t1[i],&t2[i],&t3[i],&f1[i],&f2[i]);        }        dfs(m,0);        cout<<ans<<endl;    }}


CODE:(AC)

#include <iostream>#include <algorithm>#include <string.h>#include <cmath>using namespace std;const int maxn=115;int n,m,t1[maxn],t2[maxn],t3[maxn],f1[maxn],f2[maxn];int dp[maxn][maxn];int main(){    int T;    cin>>T;    while(T--){        cin>>n>>m;        for(int i=0;i<n;i++)            cin>>t1[i]>>t2[i]>>t3[i]>>f1[i]>>f2[i];        memset(dp,120,sizeof(dp));        dp[0][m]=0;        for(int i=0;i<n;i++){            for(int j=0;j<=m;j++){                if(j>=f1[i])                    dp[i+1][j-f1[i]]=min(dp[i+1][j-f1[i]],dp[i][j]+t1[i]);                if(j+f2[i]>m)                    dp[i+1][m]=min(dp[i+1][m],dp[i][j]+t3[i]);                else                    dp[i+1][j+f2[i]]=min(dp[i+1][j+f2[i]],dp[i][j]+t3[i]);                dp[i+1][j]=min(dp[i+1][j],dp[i][j]+t2[i]);            }        }        cout<<*min_element(dp[n],dp[n]+m+1)<<endl;    }    return 0;}




0 0
原创粉丝点击