3596.   Watch The Movie

Time Limit: 2.0 Seconds   Memory Limit: 65536K
Total Runs: 424   Accepted Runs: 148

New semester is coming, and DuoDuo has to go to school tomorrow. She decides to have fun tonight and will be very busy after tonight. She like watch cartoon very much. So she wants her uncle to buy some movies and watch with her tonight. Her grandfather gave them L minutes to watch the cartoon. After that they have to go to sleep.

DuoDuo list N piece of movies from 1 to N. All of them are her favorite, and she wants her uncle buy for her. She give a value Vi (Vi > 0) of the N piece of movies. The higher value a movie gets shows that DuoDuo likes it more. Each movie has a time Ti to play over. If a movie DuoDuo choice to watch she won't stop until it goes to end.

But there is a strange problem, the shop just sell M piece of movies (not less or more then), It is difficult for her uncle to make the decision. How to select M piece of movies from N piece of DVDs that DuoDuo want to get the highest value and the time they cost not more then L. How clever you are! Please help DuoDuo's uncle.

Input

The first line of the input file contains a single integer t (1 ≤ t ≤ 10), the number of test cases, followed by input data for each test case:

The first line is: N(N ≤ 100), M(MN), L(L ≤ 1000)

N: the number of DVD that DuoDuo want buy. M: the number of DVD that the shop can sale. L: the longest time that her grandfather allowed to watch. The second line to N+1 line, each line contain two numbers. The first number is the time of the i-th DVD, and the second number is the value of ith DVD that DuoDuo rated.

Output

Contain one number (It is less than 231).

The total value that DuoDuo can get tonight. If DuoDuo can't watch all of the movies that her uncle had bought for her, please output 0.

Sample Input

1
3 2 10
11 100
1 2
9 1

Sample Output

3
题目大意:多多要看影片,然后给她的看影片的时间是固定的,然后他的叔叔去挑选影片,一共有N张影片,他需要挑选M张,
每一张影片有两个属性,一个是快乐值,一个是放完需要的时间,问你如何挑选可以使多多的快乐值最大 。
思路分析:这是一道2维背包的题目,我们可以从挑选的要求来进行分析,首先每一张光盘有着耗时,这肯定是一种代价,但是
与以往的01背包问题进行对比,可以发现,以往的背包并没有限制物品的数量,但是这道题对物品的数量进行了限制,这就是另一个
代价,同时物品的数量要求必须达到M件,即恰好装满,所以在初始化的时候需要注意。
代码:#include <iostream>
#include <stack>
#include <cstdio>
#include <cstring>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
int f[105][1010];
int v[105],w[105];
#define inf 0x1f1f1f1f
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int N,M,L;
        scanf("%d%d%d",&N,&M,&L);
        for(int i=1;i<=N;i++)
        {
            scanf("%d%d",&w[i],&v[i]);
        }
        memset(f,0,sizeof(f));
        for(int i=1;i<=M;i++)
        {
            for(int j=0;j<=L;j++)
            {
                f[i][j]=-inf;
            }
        }
        for(int i=1;i<=N;i++)
        {
            for(int j=M;j>=1;j--)
            {
                for(int k=L;k>=w[i];k--)
                {
                    f[j][k]=max(f[j-1][k-w[i]]+v[i],f[j][k]);
                }
            }
        }
      int ma=0;
      for(int i=1;i<=L;i++)
     if(ma<f[M][i]) ma=f[M][i];
     cout<<ma<<endl;
    }
}

TOJ3596 二维背包的更多相关文章

  1. 二维背包(钟神想要的)(不是DP)

    [问题描述] 背包是个好东西,希望我也有.给你一个二维的背包,它的体积是? × ?.现在你有一些大小为1× 2和1×3的物品,每个物品有自己的价值.你希望往背包里面装一些物品,使得它们的价值和最大,问 ...

  2. hdu 4501 小明系列故事——买年货_二维背包

    题目:你可以有v1元,v2代金券,v3个物品免单,现在有n个商品,商品能用纸币或者代金券购买,当然你可以买v3个商品免费.问怎么最大能买多少价值 题意: 思路二维背包,dp[v1][v2][v3]=M ...

  3. HDU 2159 FATE (二维背包)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2159 解题报告:这题实际上是一个二维的背包问题,也可以由01背包扩展而来,01背包用一维数组,可想而知 ...

  4. rqnoj-329-刘翔!加油!-二维背包

    注意排除干扰项. 因为价值不会相等,所以价值的多少与本题没有任何关系,. 所以价值为干扰项,所以不用考虑. 二维背包,简单求解. #include<stdio.h> #include< ...

  5. NOI 4978 宠物小精灵之收服(二维背包)

    http://noi.openjudge.cn/ch0206/4978/ 描述 宠物小精灵是一部讲述小智和他的搭档皮卡丘一起冒险的故事. 一天,小智和皮卡丘来到了小精灵狩猎场,里面有很多珍贵的野生宠物 ...

  6. dp之二维背包poj2576

    题意:有一群sb要拔河,把这群sb分为两拨,两拨sb数只差不能大于1,输出这两拨人的体重,小的在前面...... 思路:把总人数除2,总重量除2,之后你会发现就是个简单的二维背包,有两个限制..... ...

  7. hdu 3496 Watch The Movie (二维背包)

    Watch The Movie Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)T ...

  8. 二维背包---P1509 找啊找啊找GF

    P1509 找啊找啊找GF 题解 很明显这是一道二维背包题目 如果一个dp数组做不了,那么我们就再来一个dp数组 题目要求,花费不超过 m ,消耗人品不超过  r  ,泡到尽量多的妹子,时间尽量少 f ...

  9. 二维背包---P1855 榨取kkksc03

    P1855 榨取kkksc03 题解 二维背包板子题 f[ i ][ j ] 前 n 个物品,花费金钱不超过 i ,花费时间不超过 j 的最大价值 如果每个物品只能选一次,那么就相当于在01背包上多加 ...

随机推荐

  1. Visual Studio发布项目到远程服务器的步骤

    第一步: 需要远程服务器上安装Web Deploy ,下载地址:http://www.iis.net/downloads/microsoft/web-deploy PS.安装时选择完全安装. 第二步: ...

  2. 使用PHP对数据库输入进行恶意代码清除

    这是一个有用的PHP函数清理了所有的输入数据,并删除代码注入的几率. function sanitize_input_data($input_data) { $input_data = trim(ht ...

  3. jQuery对下拉框、单选框、多选框的处理

    下拉框: //得到下拉菜单的选中项的文本(注意中间有空格) var cc1 = $(".formc select[@name='country'] option[@selected]&quo ...

  4. MySQL查询指定时间的数据

    user_event :用户事件表 create_time :表中存储时间的字段 #获取当月数据 SELECT * FROM user_event WHERE DATE_FORMAT(create_t ...

  5. C语言初学 俩数相除问题

    #include<stdio.h> #include<stdlib.h> main() { double a,b;                       scanf(&q ...

  6. 漫谈项目设计&重构&性能优化

    重构的好处:重构能够改进软件设计,随着项目需求的变更,项目体积的变大早已与最初的设计大相径庭,代码结构变得凌乱.复杂,如果不进行重构,则很难添加新的功能. 1.使项目代码更容易理解很多情况下是由于项目 ...

  7. “express不是内部或外部命令”解决办法

    由于安装的Express是最新版本4.13.1,4.x版本就需要安装express-generator.命令如下:npm install -g express-generator ps: 1.卸载: ...

  8. UART与USART的区别

    UART与USART都是单片机上的串口通信,他们之间的区别如下: 首先从名字上看: UART:universal asynchronous receiver and transmitter通用异步收/ ...

  9. Windows脚本 - %~dp0的含义

    含义是:更改当前目录为批处理本身的目录,有些晕吧?不急,我举例 比如你有个批处理a.bat在D:\qq文件夹下  a.bat内容为 cd /d %~dp0 在这里,cd /d %~dp0的意思就是cd ...

  10. 自制单片机之十一……模数转换IC ADC0809

    我们重在实际制做,太罗嗦的内容我就不说了,只讲些跟制做有关的最精炼的知识. ADC0809是可以将我们要测量的模拟电压信号量转换为数字量从而可以进行存储或显示的一种转换IC.下面是它的管脚图和逻辑图: ...