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;
    }
    
  • 相关阅读:
    List of Examples Converting XNA 3.1 to XNA 4.0
    XNA程序开发常用到的一些代码汇总
    在WCF中使用Flag Enumerations
    能飞过海洋的却只有海鸥【转载】
    Variant类型转换成CString代码
    一种漂亮的自绘菜单
    转 在OpenCV中用cvCalibrateCamera2进行相机标定(附程序)
    COM组件设计与应用之VC6中用ATL写组件
    vc的菜单,工具栏
    (餐饮)写一些开店的经验转
  • 原文地址:https://www.cnblogs.com/juechen/p/5255976.html
Copyright © 2011-2022 走看看