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;
    }
    
  • 相关阅读:
    mysql类似oracle rownum写法
    mysql派生查询必须有别名问题记录
    MySQL逻辑架构简介
    JSON转成List结构数据
    MySQL锁表查询SQL
    Http请求传json数据中文乱码问题
    异步调用导致的不同步问题
    oracle导入Excel表文本数据
    JSP页面的注释细节
    Oracle cursor学习笔记
  • 原文地址:https://www.cnblogs.com/juechen/p/5255976.html
Copyright © 2011-2022 走看看