HDOJ题目3496Watch The Movie(二维背包)
来源:互联网 发布:c接收post数据 编辑:程序博客网 时间:2024/05/16 12:52
Watch The Movie
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 5653 Accepted Submission(s): 1791
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.
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.
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.
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
13 2 1011 1001 29 1
Sample Output
3
Source
2010 ACM-ICPC Multi-University Training Contest(6)——Host by BIT
Recommend
zhouzeyong | We have carefully selected several similar problems for you: 2955 2844 3033 2639 1712
#include<stdio.h>#include<string.h>int dp[1010][110];int main(){int n,m,l;int t;scanf("%d",&t);while(t--){int v[1010],t[1010],i,j,k;scanf("%d%d%d",&n,&m,&l);for(i=0;i<n;i++)scanf("%d%d",&t[i],&v[i]);memset(dp,0,sizeof(dp));for (j = 0; j <= l; ++j) for ( k = 0; k <= m; ++k) if (k == 0) dp[j][k] = 0; else dp[j][k] = -1e9;for(i=0;i<n;i++){for(j=l;j>=t[i];j--){for(k=1;k<=m;k++){if(dp[j][k]<dp[j-t[i]][k-1]+v[i])dp[j][k]=dp[j-t[i]][k-1]+v[i];}}}if(dp[l][m]>0)printf("%d\n",dp[l][m]);elseprintf("0\n");}}还有看到了另一种思路转自http://blog.csdn.net/zztant/article/details/17243611
有几个陷阱要注意
此题中对第一维的要求是必须选择M个物品,第二维的要求时,只要时间小于L都可以。
因此我们初始化dp, memset(dp,0x80,sizeof(dp)),是一个非常小的负数。
在背包问题中有如下规定:
如果是要求背包恰好装满的最大值,那么初始化除了dp[0][0]=0,其他都为-inf
如果没必要把背包装满,而只要求价值最大,那么直接初始化为0就可以了。
解的“连续性”决定了这个规则。
由于第二维不需要装满,因此只要在dp[M][j]中选择最大的那个即可。如果第二维必须装满,则只能选择dp[M][L]
ac代码#include<iostream>#include<cstring>#define Max(a,b) (a<b?b:a)#define Min(a,b) (a<b?a:b)using namespace std;int c_count;int N,M,L; int t[101];int val[101];/*二维背包,设dp[k][i][j]为只使用前k种物品,选择其中i种物品,花费为j时的最大收益。*//*物品总个数限制and最大花费限制*//*且最后求的是背包第一维恰好满时的最大值*//*转移方程 dp[k][i][j]=Max(dp[k][i][j],dp[k-1][i-1][j-t[m]]+val[m])*/int dp[101][1001];int main(){cin>>c_count;while(c_count--){cin>>N>>M>>L;for(int i=0;i<N;i++){cin>>t[i]>>val[i];}memset(dp,0x80,sizeof(dp));dp[0][0]=0;for(int i=0;i<N;i++){for(int j=M;j>=1;j--){for(int k=L;k>=t[i];k--){dp[j][k]=Max(dp[j-1][k-t[i]]+val[i],dp[j][k]);}}}int ans=0;for(int i=L;i>=0;i--){if(dp[M][i]>ans){ans=dp[M][i];}}cout<<ans<<endl;}return 0;}
0 0
- HDOJ题目3496Watch The Movie(二维背包)
- HDOJ 3496 Watch The Movie (二维背包)
- (DP,二维背包)Watch The Movie--HDOJ
- hdoj 3496 Watch The Movie【二维背包】【DP】
- hdoj Watch The Movie 3496 (二维01背包)好题
- hdu 3496 Watch The Movie(二维背包)
- HDU 3496 Watch The Movie (二维背包)
- 3496 Watch The Movie 二维背包
- HDU 3496 Watch The Movie 二维背包
- HDU 3496 Watch The Movie 二维背包
- HDU 3496 Watch The Movie(二维01背包)
- hud 3496 Watch The Movie(二维费用背包)
- hdu 3496 Watch The Movie(二维01背包)
- hdu 3496 Watch The Movie (二维背包)
- hdu 3496 Watch The Movie(二维01背包)
- hdu 3496 Watch The Movie(二维01背包)
- hdu 3496 Watch The Movie (二维费用背包)
- HDU-3496-Watch The Movie(二维01背包问题)
- tty、pty、pts等(小记)
- Android核心基础——S02E08_获得内存状态
- PHP第十课 PHP图像处理函数以及验证码实现
- 【iOS】苹果,百度Map定位使用与总结
- Hadoop安装教程_单机/伪分布式配置_Ubuntu 14.04/Hadoop 2.4.1
- HDOJ题目3496Watch The Movie(二维背包)
- Java迭代器深入理解及使用
- Linux企业级项目实践之网络爬虫(5)——处理配置文件
- 迁移 VMware 虚拟机到 KVM
- SQL自定义自增列(通用存储过程)
- 使用virt-p2v工具将物理机迁移到Openstack虚拟机中
- 老鸟的Python入门教程
- UVA 11573 - Ocean Currents(BFS+优先队列)
- 筛选法求素数