TZOJ 1545 Hurdles of 110m(01背包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个障碍需要跨过去,跨过障碍有3种方式:快跑T1秒,消耗F1能量;普通跑T2秒,消耗0;慢跑T3秒,获得F2能量,求跨过N个障碍需要最短的时间
题解
算是一道简单的01背包问题,Dp[i][j],i为第i个障碍,j为剩余能量
快跑:Dp[i][j-f1[i]]=min(Dp[i][j],Dp[i-1][j-f1[i]]+t1[i]);
普通:Dp[i][j]=min(Dp[i][j],Dp[i-1][j]+t2[i]);
慢跑:如果j+f2[i]<=m时,
Dp[i][j+f2[i]]=min(Dp[i][j],Dp[i-1][j+f2[i]]+t3[i]);
如果j+f2[i]>m时,这里获得能量不能超过m,多出来的能量浪费(艰难的错了WA了几次,英语渣了)
Dp[i][m]=min(Dp[i][m],Dp[i-1][j]+t3[i]);
代码
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define INF 0x3f3f3f3f
int main()
{
int t,n,m;
scanf("%d",&t);
int Dp[][];
int t1[],t2[],t3[],f1[],f2[];
while(t--)
{
memset(Dp,INF,sizeof(Dp));
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)
scanf("%d%d%d%d%d",&t1[i],&t2[i],&t3[i],&f1[i],&f2[i]);
for(int j=;j<=m;j++)
Dp[][j]=;
for(int i=;i<=n;i++)
{
for(int j=;j<=m;j++)
{
//normal
Dp[i][j]=min(Dp[i][j],Dp[i-][j]+t2[i]);
//fast
if(j-f1[i]>=)
Dp[i][j-f1[i]]=min(Dp[i][j-f1[i]],Dp[i-][j]+t1[i]);
//slow
if(j+f2[i]<=m)//不过载
Dp[i][j+f2[i]]=min(Dp[i][j+f2[i]],Dp[i-][j]+t3[i]);
else//能量过载
Dp[i][m]=min(Dp[i][m],Dp[i-][j]+t3[i]);
}
}
int minn=INF;
for(int j=;j<=m;j++)
minn=min(minn,Dp[n][j]);
printf("%d\n",minn);
}
return ;
}
TZOJ 1545 Hurdles of 110m(01背包dp)的更多相关文章
- noj [1479] How many (01背包||DP||DFS)
http://ac.nbutoj.com/Problem/view.xhtml?id=1479 [1479] How many 时间限制: 1000 ms 内存限制: 65535 K 问题描述 The ...
- PAT L3-001 凑零钱(01背包dp记录路径)
韩梅梅喜欢满宇宙到处逛街.现在她逛到了一家火星店里,发现这家店有个特别的规矩:你可以用任何星球的硬币付钱,但是绝不找零,当然也不能欠债.韩梅梅手边有104枚来自各个星球的硬币,需要请你帮她盘算一下,是 ...
- HDU 1203 I NEED A OFFER!(01 背包DP)
点我看题目 题意 : 中文题不详述. 思路 :类似于01背包的DP,就是放与不放的问题,不过这个要求概率,至少得到一份offer的反面就是一份也得不到,所以先求一份也得不到的概率,用1减掉就可以得到所 ...
- (01背包 dp)P1049 装箱问题 洛谷
题目描述 有一个箱子容量为VV(正整数,0≤V≤20000),同时有nn个物品(0<n≤30,每个物品有一个体积(正整数). 要求nn个物品中,任取若干个装入箱内,使箱子的剩余空间为最小. 输入 ...
- 0-1背包dp|波动数列|2014年蓝桥杯A组10-fishers
标题:波动数列 观察这个数列: 1 3 0 2 -1 1 -2 ... 这个数列中后一项总是比前一项增加2或者减少3. 栋栋对这种数列很好奇,他想知道长度为 n 和为 s 而且后一项总是比前一项增加a ...
- HDU 2602 Bone Collector (01背包DP)
题意:给定一个体积,和一些物品的价值和体积,问你最大的价值. 析:最基础的01背包,dp[i] 表示体积 i 时最大价值. 代码如下: #pragma comment(linker, "/S ...
- hiho #1038 : 01背包 (dp)
#1038 : 01背包 时间限制:20000ms 单点时限:1000ms 内存限制:256MB 描述 且说上一周的故事里,小Hi和小Ho费劲心思终于拿到了茫茫多的奖券!而现在,终于到了小Ho领取奖励 ...
- Bookshelf 2(poj3628,01背包,dp递推)
题目链接:Bookshelf 2(点击进入) 题目解读: 给n头牛,给出每个牛的高度h[i],给出一个书架的高度b(所有牛的高度相加>书架高度b),现在把一些牛叠起来(每头牛只能用一次,但不同的 ...
- 01背包-dp
一 问题分析 二 代码实现 package Dp_0_1_bag; import java.io.BufferedWriter; import java.io.FileWriter; import j ...
随机推荐
- Win7_64位 CHM打不开
(2)在命令行运行regsvr32 itss.dll (3)在命令行运行regsvr32 hhctrl.ocx (4)开始--运行--输入“regedit”,打开注册表,找到以下分支: HKEY_LO ...
- js获取数组长度,对象成员个数字、符串字数
文章来源:百度文库 Javascript怎么得到数组长度(也就是数组的元素个数)? Javascript怎么获取对象的成员个数? 你肯定想到了array.length!? 那么我们来测试一下下面这 ...
- MySQL concat用法举例
concat配合information_schema的应用 1 concat的一般用法主要是用于拼接 示例: 执行语句 SELECT CONCAT('M','y','S','Q','L') 可以 ...
- thinkphp 5.0手记
场景配置,可配置多个数据库,按需求加载 数组合并:array_merge();键名相同后面覆盖前面 array_merge_recursive();键名相同,键值合并 对与http://localho ...
- 自己写的jQuery颜色插件
界面效果: 插件js代码: ;(function ($) { //122种颜色 var aColors = [ "ff0000", "ffff00", &quo ...
- CGLib缺少jar出现 java.lang.ClassNotFoundException: org.objectweb.asm.Type
CGLib实现动态代理区别于JDK动态代理,不需要目标类实现任何接口,是通过生成代理类子类的方式,而且据说速度要快于JDK动态代理.所以我想要试验一下CGlib的动态代理,网上找了些例子,自己动手写了 ...
- 用docker搭建php+nginx+laravel的开发环境
制作镜像 由于官方php:7.2.2-fpm-alpine3.7 镜像不含composer,而单独做一个composer镜像又会依赖php镜像,所以应该在php镜像中添加composer.Docker ...
- 【求助】win 2008 R2 远程桌面多用户,破解最大连接数2的限制
[求助]win 2008 R2 远程桌面多用户,破解最大连接数2的限制. 1. 本地组策略设置的是“允许的RD最大连接数 5”. 2. 远程桌面仍然只能有两个连接在线. 3. 后来发现是下面这个设置限 ...
- Python自定义状态码枚举类
在Java里很容易做到自定义有状态码和状态说明的枚举类例如: public enum MyStatus { NOT_FOUND(404, "Required resource is not ...
- 关于QT中“崩溃”问题
经常会遇到一个问题,程序运行崩溃! 1.release.debug直接运行崩溃. 2.程序可以运行但是点击界面崩溃. 3.debug模式崩溃,release正常. 4.软件里面的release和deb ...