您的位置:首页 > 其它

ZOJ 2972-Hurdles of 110m(背包dp)

2015-11-08 20:51 405 查看
H - Hurdles of 110m
Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld
& %llu
Submit Status Practice ZOJ
2972

Description

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.

Input

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.

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


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

参考资料:
      转自:蚂蚁大战大象

题意:刘翔跨栏问题,初始有M能量,有N块区域需要跑,在第i可以使用3种模式:

1.Fast模式 通过第i个区域需要用T1[i]的时间,需要消耗F1[i]能量.

2.Normal模式 通过第i个区域需要用T2[i]的时间,不需要消耗能量.

3.Slow模式 通过第i个屈戌需要T3[i]的时间,能增加F2[i]能量,但是增加后的能量不能超过总能量M.

求通过N个区域的最短时间.

还是比较简单的DP,阶段和决策很清楚,写起来很流畅.

设dp[i][j]为跑过前i个区域剩余能量为j时的最少用时.

那么dp[0][j]自然就是0,答案就是min{dp
[j] | 0<= j <= M}

1.如果在第i区域用Fast模式,那么dp[i + 1][j - F1[i]] = min(dp[i + 1][j - F1[i]], dp[i][j] + T1[i]),注意这里的j - F1[i]不能小于0.

2.如果在第i区域用Normal模式,那么dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + T2[i]).

3.如果在第i区域用Slow模式,那么dp[i + 1][j + F2[i]] = min(dp[i + 1][j + F2[i]], dp[i][j] + T2[i]),注意这里的j + F2[i]如果超过了M,那么要变成M.

   这题不参考上面的资料都不知道那个dp的动态转移方程是如何弄出来的,看一下子就看懂了,但要自己亲自去写的话却无从下手。。。。或许是dp没训练过吧,所以脑袋抽了。

AC代码:

#include<iostream>
#include<sstream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cstdio>
#include<stack>
using namespace std;
#define T 500
#define inf 0x3f3f3f3f
int main()
{
#ifdef zsc
freopen("input.txt","r",stdin);
#endif
int N,n,m,i,j;
int dp[T][T];
int T1[T],T2[T],T3[T],F1[T],F2[T];
scanf("%d",&N);
while(N--)
{
scanf("%d%d",&n,&m);
for(i=0;i<n;++i){
scanf("%d%d%d%d%d",&T1[i],&T2[i],&T3[i],&F1[i],&F2[i]);
}
memset(dp,inf,sizeof(dp));
for(i=0;i<=m;++i){
dp[0][i] = 0;
}
int ans = inf;
for(i=0;i<n;++i){
for(j=0;j<=m;++j){
if(j>=F1[i]&&dp[i+1][j-F1[i]]>dp[i][j]+T1[i]){//第一种
dp[i+1][j-F1[i]] = dp[i][j]+T1[i];
}
if(dp[i+1][j]>dp[i][j]+T2[i]){//第二种
dp[i+1][j] = dp[i][j] + T2[i];
}
int tmp = min(m,j+F2[i]);
if(dp[i+1][tmp]>dp[i][j]+T3[i]){//第三种
dp[i+1][tmp] = dp[i][j] + T3[i];
}
}
}
for(i=0;i<=m;++i){
ans = min(ans,dp
[i]);
}
printf("%d\n",ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  dp zoj