【TOJ 1545】Hurdles of 110m(背包题)

描述

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秒,不消耗能量;

③慢跑T3秒,获得F2能量;

求跨过N个障碍所需要的最短时间。

#include<bits/stdc++.h>
#define INF 0x3f3f3f3f
using namespace std;
int main()
{
    int t,n,m,i,j,dp[115][115];
    int t1[115],t2[115],t3[115],f1[115],f2[115];
    cin>>t;
    while(t--)
    {
        int minn=INF;
        memset(dp,INF,sizeof(dp));  //把数组中每个元素设置为无穷大
        cin>>n>>m;
        for(i=1;i<=n;i++)
            scanf("%d%d%d%d%d",&t1[i],&t2[i],&t3[i],&f1[i],&f2[i]);
        for(j=0;j<=m;j++)
            dp[0][j]=0;
        for(i=1;i<=n;i++)
        {
            for(j=0;j<=m;j++)
            {
                //normal
                dp[i][j]=min(dp[i][j],dp[i-1][j]+t2[i]);

                //fast
                if(j-f1[i]>=0)
                    dp[i][j-f1[i]]=min(dp[i][j-f1[i]],dp[i-1][j]+t1[i]);

                //slow
                if(j+f2[i]<=m) //能量有余
                    dp[i][j+f2[i]]=min(dp[i][j+f2[i]],dp[i-1][j]+t3[i]);
                else           //能量过剩
                    dp[i][m]=min(dp[i][m],dp[i-1][j]+t3[i]);
            }
        }
        for(j=0;j<=m;j++)
            minn=min(minn,dp[n][j]);
        printf("%d\n",minn);
    }
    return 0;
}

原文地址:https://www.cnblogs.com/kannyi/p/9043596.html

时间: 2024-10-18 00:20:14

【TOJ 1545】Hurdles of 110m(背包题)的相关文章

Hurdles of 110m 【DP 背包】

一共有N段过程,每段过程里可以选择 快速跑. 匀速跑 和 慢速跑 对于快速跑会消耗F1 的能量, 慢速跑会集聚F2的能量 选手一开始有M的能量,即能量上限 求通过全程的最短时间 定义DP[i][j] 为跨越第 i 个栏,剩余 j 点能量 动态转移方程 dp[i][j] = min(dp[i][j], dp[i-1][j-F1]+T1) (Fast Mode) dp[i][j] = min(dp[i][j], dp[i-1][j]+T2) (Normal Mode) dp[i][j] = min(

TOJ礼品兑换 (多重背包恰好装满)

近期crq老师为了提高各个学生对ACM的兴趣,在TOJ上增加了积分制度和礼品兑换功能, TOJ的积分是来之不易的,固然同学们都想用同一积分换取最大价值的礼品,某同学用了M的积分换取了一些礼品. 请问:他用M的积分最多能换取多少价值的礼品呢? (积分M一定要使用完) 输入 输入数据首先包含一个正整数C,表示有C组测试用例,每组测试用例的第一行是两个整数M和N(1<=M<=1000,1<=N<=100),分别表示换取的总积分和礼品的种类,然后是N行数据,每行包含3个数p,h和c(1&l

ZOJ 2972 Hurdles of 110m

简单DP.dp[i][j]表示完成第i段,有j体力的情况下,获得的最小时间, 然后就可以递推: //高速跑 if(j-s[i].f1>=0) dp[i][j-s[i].f1]=min(dp[i][j-s[i].f1],dp[i-1][j]+s[i].t1); //中速跑 dp[i][j]=min(dp[i][j],dp[i-1][j]+s[i].t2); //低速跑 dp[i][min(m,j+s[i].f2)]=min(dp[i][min(m,j+s[i].f2)],dp[i-1][j]+s[

杭电ACM分类

杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze 广度搜索1006 Redraiment猜想 数论:容斥定理1007 童年生活二三事 递推题1008 University 简单hash1009 目标柏林 简单模拟题1010 Rails 模拟题(堆栈)1011 Box of Bricks 简单题1012 IMMEDIATE DECODABILITY

【转】对于杭电OJ题目的分类

[好像博客园不能直接转载,所以我复制过来了..] 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze 广度搜索1006 Redraiment猜想 数论:容斥定理1007 童年生活二三事 递推题1008 University 简单hash1009 目标柏林 简单模拟题1010 Rails 模拟题(堆栈)1011 Box of Bricks 简单题1012 IMMEDI

转载:hdu 题目分类 (侵删)

转载:from http://blog.csdn.net/qq_28236309/article/details/47818349 基础题:1000.1001.1004.1005.1008.1012.1013.1014.1017.1019.1021.1028.1029. 1032.1037.1040.1048.1056.1058.1061.1070.1076.1089.1090.1091.1092.1093. 1094.1095.1096.1097.1098.1106.1108.1157.116

HDU 2546 饭卡(01背包裸题)

饭卡 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 28562    Accepted Submission(s): 9876 Problem Description 电子科大本部食堂的饭卡有一种很诡异的设计,即在购买之前判断余额.如果购买一个商品之前,卡上的剩余金额大于或等于5元,就一定可以购买成功(即使购买后卡上余额为负),否则无

【动态规划】背包问题(一) 01背包 完全背包 多重背包

一.01背包 有N件物品和一个容量为V的背包.第i件物品的价格(即体积,下同)是w[i],价值是c[i].求解将哪些物品装入背包可使这些物品的费用总和不超过背包容量,且价值总和最大. 这是最基础的背包问题,总的来说就是:选还是不选,这是个问题<( ̄ˇ ̄)/ 相当于用f[i][j]表示前i个背包装入容量为v的背包中所可以获得的最大价值. 对于一个物品,只有两种情况 情况一: 第i件不放进去,这时所得价值为:f[i-1][v] 情况二: 第i件放进去,这时所得价值为:f[i-1][v-c[i]]+w

hdu 2602 Bone Collector(01背包)

Bone Collector Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 42179    Accepted Submission(s): 17543 Problem Description Many years ago , in Teddy’s hometown there was a man who was called “Bon