zoukankan      html  css  js  c++  java
  • hdu3496

    题意:从N部电影中选M部看,每部都有不同的时长和价值,问是否可以看完M部电影,可以的话求价值的最大值,否则输出0.

    分析:这是有两重费用的01背包,其实原本就是在01背包的基础上加多一维就可以了。这题主要卡在"If DuoDuo can’t watch all of the movies that her uncle had bought for her, please output 0.",因此每个Knapsack[费用][费用]都是要刚刚好用到这么多,因此在输出的时候答案不一定是Knapsack[m][l],而是在Knapsack[m][?]里的其中一个。

    #include <cstdio>
    #include <algorithm>
    int knapsack[101][1001],n,m,l,t;
    int value[101],len[101];
    int main()
    {
        scanf("%d",&t);
        while(t--)
        {
            scanf("%d%d%d",&n,&m,&l);
            for(int i = 1;i <= n;i++)
                scanf("%d%d",&len[i],&value[i]);
            for(int k = 0;k <= m;k++)
            {
                for(int j = 0;j <= l;j++)
                    knapsack[k][j] = -1;
            }
            knapsack[0][0] = 0;
            for(int i = 1;i <= n;i++)
            {
                for(int k = m;k >= 1;k--)
                {
                    for(int j = l;j >= len[i];j--)
                    {
                        if(knapsack[k - 1][j - len[i]] >= 0 && knapsack[k][j] <= knapsack[k - 1][j - len[i]] + value[i])
                            knapsack[k][j] = knapsack[k - 1][j - len[i]] + value[i];
                    }
                }
            }
            int max = -1;
            for(int j = 1;j <= l;j++)
                max = std::max(max,knapsack[m][j]);
            if(max < 0)
                printf("0\n");
            else
                printf("%d\n",max);
        }
        return 0;
    }
  • 相关阅读:
    Sicily shortest path in unweighted graph
    Sicily connect components in undirected graph
    Sicily 1931. 卡片游戏
    Sicily 1021. Couples
    c++ 高效文本读写
    Sicily 1129. ISBN
    Sicily 1133. SPAM
    Sicily 1282. Computer Game
    小工具?不,这是小工具的集合!
    .Net Core建站(4):FTP发布项目及连接服务器数据库
  • 原文地址:https://www.cnblogs.com/ZShogg/p/3067571.html
Copyright © 2011-2022 走看看