Bone Collecto(01背包)

Problem Description

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 ?

Bone Collecto(01背包)

 

 

 

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 231).

 

 

Sample Input

 

1 5 10 1 2 3 4 5 5 4 3 2 1

 

 

Sample Output

 

14

关于01背包,我还不是很了解,大致看了下书本,主要是一个二维递推式。而这个问题就是个基本的01背包问题,我自己尝试写了下,总是WA,原因也不知道是什么(难受),后来参考了别人的博客,发现基本上都是一维数组递推,在空间上做了优化。

附上我WA的代码(有么有大佬帮我看看为什么错,我感觉没问题啊)

#include<iostream>
#include<vector>
#include<string>
#include<cstring>
#include<cmath>
#include <algorithm>
#include <stdlib.h>
#include <cstdio>
#include<sstream>
#include<cctype>
#include <set>
#include<queue>
#include <map>
#include <iomanip>
#define  INF  0x3f3f3f3f
typedef long long ll;
using namespace std;
ll  valu[1010];
ll  weight[1010];
ll dp[1010][1012];
int main()
{
     //freopen("input.txt","r",stdin);
    ll t,n,m;
    scanf("%lld",&t);
    for(ll i=1;i<=t;i++)
    {

        memset(dp,0,sizeof(dp));
        memset(valu,0,sizeof(valu));
        memset(weight,0,sizeof(weight));
        scanf("%lld%lld",&n,&m);
        for(ll j=1;j<=n;j++)
        {
          scanf("%lld",&valu[j]);
        }
        for(ll j=1;j<=n;j++)
        {
             scanf("%lld",&weight[j]);
        }
        for(ll l=1;l<=n;l++)
        {
            for(ll k=1;k<=m;k++)
            {


                    dp[l][k]=max(dp[l-1][k],dp[l-1][k-weight[l]]+valu[l]);

            }
        }

        printf("%lld\n",dp[n][m]);

    }
    return 0;
}

AC代码:

#include<iostream>
#include<vector>
#include<string>
#include<cstring>
#include<cmath>
#include <algorithm>
#include <stdlib.h>
#include <cstdio>
#include<sstream>
#include<cctype>
#include <set>
#include<queue>
#include <map>
#include <iomanip>
#define  INF  0x3f3f3f3f
typedef long long ll;
using namespace std;
ll  valu[1010];
ll  weight[1010];
ll dp[1010];
int main()
{
     //freopen("input.txt","r",stdin);
    ll t,n,m;
    scanf("%lld",&t);
    for(ll i=1;i<=t;i++)
    {

        memset(dp,0,sizeof(dp));
        memset(valu,0,sizeof(valu));
        memset(weight,0,sizeof(weight));
        scanf("%lld%lld",&n,&m);
        for(ll j=1;j<= n;j++)
        {
          scanf("%lld",&valu[j]);
        }
        for(ll j=1;j<=n;j++)
        {
             scanf("%lld",&weight[j]);
        }
         for(ll k=1 ; k<=n ; k++)

		{
			for(ll j=m ; j>=weight[k] ; j--)

			{

				dp[j] = max(dp[j],dp[j-weight[k]]+valu[k]);

			}
		}
        printf("%lld\n",dp[m]);

            }
    return 0;
}