• TOJ3596 二维背包



    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;
        }
    }
  • 相关阅读:
    索引
    convert 时间转换
    SQL中的case when then else end用法
    SVN安装图解
    dos 命令
    阿里云服务器相关知识
    mvc知识应用
    MVC分页
    mvc系统过滤器
    .net 接口
  • 原文地址:https://www.cnblogs.com/xuejianye/p/5425896.html
Copyright © 2020-2023  润新知