HDU_3496_(二维费用背包)

Watch The Movie

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 7585    Accepted Submission(s): 2427

Problem Description

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(M<=N),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 ith DVD, and the second number is the value of ith DVD that DuoDuo rated.

Output

Contain one number. (It is less then 2^31.)
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

题意:duoduo给N部片打了分,可以看L分钟片,店里只一次性卖M部片(最开始忽略了这个条件),每部片有长度,问duoduo在L分钟内看完这M部片可以的到的最高分。

读清了题还是不难。

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;

int dp[1005][105];
int cost,val;
int n,m,l;

void zero_one(int cost,int val)
{

    for(int j=l;j>=cost;j--)
        for(int k=m;k>=1;k--)
            if(dp[j-cost][k-1]>=0)
                dp[j][k]=max(dp[j][k],dp[j-cost][k-1]+val);
}
int main()
{

    int t;
    scanf("%d",&t);
    while(t--)
    {
        memset(dp,-1,sizeof(dp));
        dp[0][0]=0;
        scanf("%d%d%d",&n,&m,&l);
        for(int i=0;i<n;i++)
        {
            scanf("%d%d",&cost,&val);
            zero_one(cost,val);
        }
        int res=0;
        for(int i=1;i<=l;i++)
            if(res<dp[i][m])
                res=dp[i][m];
        printf("%d\n",res);
    }
    return 0;
}
时间: 2024-12-09 03:33:24

HDU_3496_(二维费用背包)的相关文章

洛谷 P1509 找啊找啊找GF(复习二维费用背包)

传送门 题目背景 "找啊找啊找GF,找到一个好GF,吃顿饭啊拉拉手,你是我的好GF.再见." "诶,别再见啊..." 七夕...七夕...七夕这个日子,对于sqybi这种单身的菜鸟来说是多么的痛苦...虽然他听着这首叫做"找啊找啊找GF"的歌,他还是很痛苦.为了避免这种痛苦,sqybi决定要给自己找点事情干.他去找到了七夕模拟赛的负责人zmc MM,让她给自己一个出题的任务.经过几天的死缠烂打,zmc MM终于同意了. 但是,拿到这个任务的sqy

codevs1959拔河比赛(二维费用背包)

1959 拔河比赛 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 一个学校举行拔河比赛,所有的人被分成了两组,每个人必须(且只能够)在其中的一组,要求两个组的人数相差不能超过1,且两个组内的所有人体重加起来尽可能地接近. 输入描述 Input Description 数据的第1行是一个n,表示参加拔河比赛的总人数,n<=100,接下来的n行表示第1到第n个人的体重,每个人的体重都是整数(1<=weight<=450).

POJ2184Cow Exhibition(二维费用背包)

Cow Exhibition Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9067   Accepted: 3441 Description "Fat and docile, big and dumb, they look so stupid, they aren't much fun..." - Cows with Guns by Dana Lyons The cows want to prove to t

HDU2159 二维费用背包

题目链接:FATE 状态转移方程: dp[ren][num] =max(dp[ren-耐久值][num-1]+ 经验值,dp[ren][num]) dp表示:当前忍耐度ren下杀敌数为num的经验值 枚举分别枚举 所有怪物种类.耐久度.杀怪数 最后在从小到达枚举消耗的耐久度即可 #include <iostream> #include <algorithm> #include <cstdio> #include <cstring> #include <

hdu3496+poj1948(二维费用背包)

Watch The Movie Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Total Submission(s): 5106    Accepted Submission(s): 1614 Problem Description New semester is coming, and DuoDuo has to go to school tomorrow. She dec

[二维费用背包DP]找啊找啊找GF

题目链接 思考 首先题目一定是背包DP(多读数据范围和题意) 其次一定是 二维费用的背包问题 (人品和金钱) 最后题目要求的是 在泡尽量多的妹子的情况下,花费最少的时间. DP转移方程一定是二维的没错,但是要满足花费最少妹子最多的这个要求就比较难以解决了.不过也不要想这么多,先看看我的分析. 假设在求解过程中如果花X元RMP,Y单位RP可以到Z个MM,那么在泡第i个MM时,发现可以用X-rmb[i]元,Y-rp[i]单位RP泡到的MM数加上这个MM(也就是+1)比原来Z多,就替换它(因为你的原则

分组背包+二维费用背包

题目:https://www.acwing.com/problem/ 分组背包问题描述是共有n组物品,每组物品你只能选一个,求最大价值 1 #include<iostream> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 const int N=110; 6 struct node 7 { 8 int v,w; 9 }; 10 node wp[N]; 11 int n,m; 12

hdu 5677 ztr loves substring 二维费用背包+回文

题目链接: hdu 5677 ztr loves substring 官方题解: //这部分是错的(首先,对于每一个串i跑一次manancher,令g[i][j]=p[j]-1g[i][j]=p[j]−1 这样,g就存储了所有的回文子串的长度 为了方便,把g降到一维表示) 首先,因为字符串长度较小,所以直接二重for循环找就好了,用一个数组 g记录各个回文串的长度 不妨把每一个子串抽象成一个物品 费用为二维的,即{长度,1} 价值是Bool型的 这样就成了一个二维判断可行性费用背包问题 设f(i

HDU2159_FATE【二维费用背包】【完全背包】

FATE Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 8112    Accepted Submission(s): 3776 Problem Description 最近xhd正在玩一款叫做FATE的游戏,为了得到极品装备,xhd在不停的杀怪做任务.久而久之xhd开始对杀怪产生的厌恶感,但又不得不通过杀怪来升完这最后一级.现在的问