美文网首页
01背包问题

01背包问题

作者: 与卿__歌 | 来源:发表于2017-02-21 18:53 被阅读0次

A - Bone Collector

Many years ago , in Teddy’s hometown there was a man who was called “Bone Collector”. This man like to collect varies of bones , such as dog’s , cow’s , also he went to the grave … The bone collector had a big bag with a volume of V ,and along his trip of collecting there are a lot of bones , obviously , different bone has different value and different volume, now given the each bone’s value along his trip , can you calculate out the maximum of the total value the bone collector can get ? !
Input
The first line contain a integer T , the number of cases. Followed by T cases , each case three lines , the first line contain two integer N , V, (N <= 1000 , V <= 1000 )representing the number of bones and the volume of his bag. And the second line contain N integers representing the value of each bone. The third line contain N integers representing the volume of each bone.
Output
One integer per line representing the maximum of the total value (this number will be less than 2 31
).
Sample Input
15 101 2 3 4 55 4 3 2 1

Sample Output
14


//核心代码
//          for(i = 1; i<=n; i++)
//        {
 //           for(j = v;j>=s[i];j--)
 //          {
 //              dp[j] = max(dp[j],dp[j-s[i]]+x[i]);
             }
//         }

#include<stdio.h>
#include <algorithm>
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
    int t,i,j,n,v;
    int x[1005],s[1005],dp[1005];
    scanf("%d",&t);
    while(t--)
    {
         memset(dp,0,sizeof(dp));
        scanf("%d%d",&n,&v);
        for(i=1;i<=n;i++)
        {
            scanf("%d",&x[i]);
        }
        for(j=1;j<=n;j++)
        {
            scanf("%d",&s[j]);
        }
        for(i = 1; i<=n; i++)
        {
            for(j = v;j>=s[i];j--)
            {
                dp[j] = max(dp[j],dp[j-s[i]]+x[i]);
            }
        }
        printf("%d\n",dp[v]);
    }
}

相关文章

  • 动态规划-背包问题

    01背包问题 详解:01背包问题详解链接

  • 背包问题1(01背包)

    N件物品,没见有重量Wi,价值Vi;选其中几件放入容量为M的背包中,求价值的最值。——经典背包问题背包问题分三类:...

  • 01背包问题

    动态规划算法一般用来求解最优化问题,当问题有很多可行解,而题目要求寻找这些解当中的“最大值”/“最小值”时,通常可...

  • 01背包问题

    题目描述:给定 n 个物品和一个容量为 W 的背包,物品 i 的重量是 wi,其价值为 vi 。应该如何选择装入背...

  • 01背包问题

    有n个重量和价值分别为wi,vi的物品。从这些物体中挑选出总重量不超过W的物品,求所有方案中价值总和的最大值。 1...

  • 01背包问题

    A - Bone Collector Many years ago , in Teddy’s hometown t...

  • 01背包问题

    令V(i,j)表示在前i(1<=i<=n)个物品中能够装入容量为就j(1<=j<=C)的背包中的物品的最大价值,则...

  • 01背包问题

    题目:有A(2kg,6$);B(2kg,3$);C(6kg,5$);D(5kg,4$);E(4kg,6$)五种物品...

  • 01背包问题

    题目: 有限个数的货物,具有不同的体积还有价值,怎么让其放进有限体积的背包并价值最大。 货物只有两种可能,放进去/...

  • 背包01问题

    背包01问题 背包01问题是一个经典的算法。 问题是这样描述的:一个背包最大容量为9kg,现在有5个物品,每个物品...

网友评论

      本文标题:01背包问题

      本文链接:https://www.haomeiwen.com/subject/axiuwttx.html