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
 

Author
Teddy
 

Source
 

Recommend
lcy   |   We have carefully selected several similar problems for you:  1203 2159 2955 1171 2191 
 
RE: 01背包基础题:
 
//感觉这个比较好理解:   46ms  
#include <cstdio>
#include <cstring>
#include <iostream>
#define max(a, b) a>b?a:b
using namespace std;
int dp[1010][1010], val[1010], vol[1010];
int main()
{
    int t;
    scanf("%d", &t);
    while(t--)
    {
        int n, v;
        scanf("%d %d", &n, &v);
        for(int i = 1; i <= n; i++)
            scanf("%d", &val[i]);
        for(int i = 1; i <= n; i++)
            scanf("%d", &vol[i]);
        for(int i = 1; i <= n; i++)
        {
            for(int j = 0; j <= v; j++)  //容量; 
            {
                if(j < vol[i])
                    dp[i][j] = dp[i-1][j];
                else
                    dp[i][j] = max(dp[i-1][j], dp[i-1][j-vol[i]] + val[i]);    
            } 
        }
        printf("%d\n", dp[n][v]);
    }
    return 0;    
} 

 

//比二维数组稍微快点; 31ms 
#include <cstdio>
#include <cstring>
#include <iostream>
#define max(a, b) a>b?a:b 
using namespace std;
int dp[1010], val[1010], vol[1010];
int main()
{
    int t;
    scanf("%d", &t);
    while(t--)
    {
        int n, v;
        scanf("%d %d", &n, &v);
        for(int i = 1; i <= n; i++)
            scanf("%d", &val[i]);
        for(int j = 1; j <= n; j++)
            scanf("%d", &vol[j]);
        memset(dp, 0, sizeof(dp)); 
        for(int k = 1; k <= n; k++)
        {
            for(int Q = v; Q >= vol[k]; Q--)
            {
                dp[Q] = max(dp[Q], dp[Q - vol[k]] + val[k]); 
            } 
            
        }
        printf("%d\n", dp[v]);
    }
    return 0;    
} 

 

posted on 2015-08-27 14:42  cleverbiger  阅读(134)  评论(0编辑  收藏  举报