zoukankan      html  css  js  c++  java
  • Accepted Necklace

    Accepted Necklace
    Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 3136 Accepted Submission(s): 1213

    Problem Description
    I have N precious stones, and plan to use K of them to make a necklace for my mother, but she won’t accept a necklace which is too heavy. Given the value and the weight of each precious stone, please help me find out the most valuable necklace my mother will accept.

    Input
    The first line of input is the number of cases.
    For each case, the first line contains two integers N (N <= 20), the total number of stones, and K (K <= N), the exact number of stones to make a necklace.
    Then N lines follow, each containing two integers: a (a<=1000), representing the value of each precious stone, and b (b<=1000), its weight.
    The last line of each case contains an integer W, the maximum weight my mother will accept, W <= 1000.

    Output
    For each case, output the highest possible value of the necklace.

    Sample Input

    1
    2 1
    1 1
    1 1
    3

    Sample Output

    1

    背包问题,DP[i][m][k]表示选第i件物品时的体积为m,以选定的物品为k件,所以Dp[i][m][k]=max(Dp[i-1][m][k],Dp[i-1][m-v[i]][k-1]+w[i]);再将其转化为01背包

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <queue>
    #include <algorithm>
    using namespace std;
    typedef long long LL;
    typedef pair<int,int>p;
    const int MAX =  1100;
    int Dp[MAX][35];
    p Th[35];
    int main()
    {
        int n,k,m;
        int T;
        scanf("%d",&T);
        while(T--)
        {
            scanf("%d %d",&n,&k);
            for(int i=1;i<=n;i++)
            {
                scanf("%d %d",&Th[i].first,&Th[i].second);
            }
            scanf("%d",&m);
            memset(Dp,0,sizeof(Dp));
            for(int i=1;i<=n;i++)
            {
                for(int j=m;j>=Th[i].second;j--)
                {
                    for(int s=1;s<=k;s++)
                    {
                        Dp[j][s]=max(Dp[j-Th[i].second][s-1]+Th[i].first,Dp[j][s]);
                    }
                }
            }
            printf("%d
    ",Dp[m][k]);
        }
        return 0;
    }
    
  • 相关阅读:
    动态SQL和PL/SQL的EXECUTE选项分析
    PL/SQL开发中动态SQL的使用方法
    windows 快捷调用
    Windows PE 工具
    面向对象(OOP)五大基本原则
    图像几何变换(geometric transformation)
    BP神经网络模型及梯度下降法
    人工神经元模型及常见激活函数
    Python: PS 滤镜--高反差保留 (High pass)
    Python: PS 滤镜--碎片特效
  • 原文地址:https://www.cnblogs.com/juechen/p/5255976.html
Copyright © 2011-2022 走看看