hdu3496Watch The Movie 二维背包

来源:互联网 发布:网络女主播直播吃饭 编辑:程序博客网 时间:2024/04/30 06:10

二维背包其实就是相当 与2个背包   只不过是多了一个循环

下面的程序设计当了背包的恰好问题    很值得看看    

Watch The Movie

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


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
13 2 1011 1001 29 1
 

Sample Output
3
 
 题意是 DuoDuo 想看n部电影,但是被要求最长能看的总时间数为 L ,每部电影有他的时长和DuoDuo对他的评价值 两个属性。 但是 商店有个奇怪的要求 一次只卖恰好 m 个电影碟 (m<=n) 在总时间内,为了获得最大的观赏总价值,要求得该总价值。

 

属于二维背包题目, 一个背包为 以时间为容量 另一个以数量为容量。


 
 
 
 
 
 
#include<stdio.h>
#include<string.h>
int
ti[105],val[105],maxtime,dp[1020][102];
void
bag(int n,int m)
{

    int
i,j,k,ans;
    memset(dp,-1,sizeof(dp));//由于 把要能买的光碟数即m抽象为一个背包 对于这个背包是必须要装满的 所以是背包中的‘恰好’问题
    dp[0][0]=0;//所以除了这个之外要全部赋值为负无穷 这里赋予-1 是一样的 不是-1 下面的-1可以改成>=0
    for(i=0;i<n;i++)
        for
(
k=m;k>=1;k--)//第一个背包
            for(j=maxtime;j>=ti[i];j--)//第二个背包
                if(dp[j-ti[i]][k-1]!=-1&&dp[j][k]<dp[j-ti[i]][k-1]+val[i])
                /*注意这句话 dp[j-ti[i]][k-1]!=-1 如果等于了
                dp[j-ti[i]][k-1]+val[i] 的值就受影响了 此时加上的val[i]值就少了1     以前01背包等是0 不会受影响*/

                dp[j][k]=dp[j-ti[i]][k-1]+val[i];
  //     if(dp[maxtime][m]<0) printf("0\n");/*不能用这几句 必须用下面的 不知道为什么 可能是二维背包中最大值不一定在最后把*/
    //  else
    //    printf("%d\n",dp[maxtime][m]);
               ans=0;
               for
(
i=1;i<=maxtime;i++)
                    ans=ans>dp[i][m]?ans:dp[i][m];
              printf("%d\n",ans);


}

int
main()
{

    int
i,t,n,m;
    scanf("%d",&t);
    while
(
t--)
    {

        scanf("%d %d %d",&n,&m,&maxtime);
        for
(
i=0;i<n;i++)
            scanf("%d %d",&ti[i],&val[i]);
        bag(n,m);

    }

    return
0;
}
原创粉丝点击