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;
    }
    
  • 相关阅读:
    样式
    样式表的类别、选择器和优先级
    随记
    框架
    表单元素
    HTLM内容容器标签和常用标签
    HTML5的意义、改变以及全局属性
    11月21日html基础
    感想 目标和展望
    C++结构体实例和类实例的初始化
  • 原文地址:https://www.cnblogs.com/juechen/p/5255976.html
Copyright © 2011-2022 走看看