题目

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

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

翻译

描述

很多年前,在 Teddy 的家乡有一个“集骨者”的传说
“集骨者”会收集各种各样的骨头,狗、牛……他甚至会进入坟墓来获取骨头
“集骨者”有一个容量为 V 的背包,显然在他的旅行中,这就是他放骨头的地方
不同的骨头价值不同,体积也不同,现在给你每一个骨头的价值和体积,你能计算出在容量许可范围能,最多能收集多大价值的骨头么?

输入

第一行是 T 数据组数
每组数据有一个 NV ( N <= 1000, V <= 1000)分别是骨头的数量和背包的最大容量
接下来一行有 N 个整数,表示骨头的价值
下面一行也有 N 个整数,表示骨头的体积

输出

一行不超过232的整数

题解

标准的>背包问题 - 01背包问题<

标准到不能更标准的01背包问题

代码

/*
By:OhYee
Github:OhYee
Blog:http://www.oyohyee.com/
Email:oyohyee@oyohyee.com

かしこいかわいい?
エリーチカ!
要写出来Хорошо的代码哦~
*/
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <functional>
using namespace std;

const int maxn = 1005;

int v,n;
int dp[maxn];
int value[maxn];
int vol[maxn];

void ZeroOnePack(int cost,int weight) {
    for(int i = v; i >= cost; i--)
        dp[i] = max(dp[i],dp[i - cost] + weight);
}


void Do() {
    scanf("%d%d",&n,&v);

    for(int i = 1;i <= n;i++)
        scanf("%d",&value[i]);
    for(int i = 1;i <= n;i++)
        scanf("%d",&vol[i]);
    memset(dp,0,sizeof(dp));
    for(int i = 1;i <= n;i++)
        ZeroOnePack(vol[i],value[i]);

    printf("%d\n",dp[v]);

}


int main() {
    int T;
    scanf("%d",&T);
    while(T--)
        Do();
    return 0;
}