ZOJ 2972 TOJ 1545 Hurdles of 110m(一般dp)

描述

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.

样例输入

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

样例输出

1
6

提示

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

思路分析

题目的意思是,将一个过程分为n段,然后每个人有初始能量m,对于每一段来讲,每个人都有三种状态1.通过这一段花费的时间为t1,但是需要消耗f1的能量2.通过这段需要花费的时间为t2,但是不需要消耗能量3.通过这一段需要花费的时间为t3,并且可以恢复f2的能量。

如果暴力解题的话,最大是3的110次,显然不太合理。而且也没有规律可循,因此可以想到这道题应该使用dp去解决的。可以简单想到是对于每一阶段的每一种能量状态,他都有三种模式可以选择,快速+消耗能量,中速+不变,低速+回复能量,又因为能量的上限是规定的,因此只要一个二维的dp[m][n]数组即可,纵坐标表示第几阶段,横坐标表示当前有多少能量,从第0阶段开始(从头开始的意思),对这个阶段处理完,就是经过了第一个阶段后的状态。

先将整个dp数组全部初始化为最大值

然后对个阶段的每种能量状况dp[i][j]的时间进行处理

高速,则判断是否有充足的能量,如果有,则dp[i+1][j-消耗能量]=dp[i][j]+t1;

中速,dp[i+1][j]=dp[i][j]+t2;

慢速,dp[i+1][min(j+(回复能量),最大能量)]=dp[i][j]+t3;

然后求出来的每个值都和当前值比较,取小即可。

然后遍历一遍最后阶段的每种能量情况下的消耗时间,取最小值即可。

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int N = 1e2+15;
const int INF = 0x3f3f3f3f;
int dp[N][N];//记录时间,横代表第几次,纵代表剩余能量。 
int min(int x,int y){
	return (x>y)?y:x;
}
int main() {
	int t,m,n,f1,f2,t1,t2,t3,i,j;
	cin>>t;
	while(t--){
		memset(dp,INF,sizeof(dp));
		memset(dp[0],0,sizeof(dp[0]));
		cin>>n>>m;
		for(i=0;i<n;i++){
			cin>>t1>>t2>>t3>>f1>>f2;
			for(j=0;j<=m;j++){
				int k=j+f2;
				k=min(k,m); 
				dp[i+1][k] = min(dp[i+1][k], dp[i][j]+t3);//慢速 
				dp[i+1][j] = min(dp[i+1][j], dp[i][j]+t2);//中速 
				if(j>=f1) dp[i+1][j-f1] = min(dp[i+1][j-f1], dp[i][j]+t1);//快速 
			} 
		}
		int T=INF;
		for(int i=0;i<=m;i++){
			T=min(dp[n][i], T); 
		}
		cout<<T<<endl; 
	}
	return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值