必威体育Betway必威体育官网
当前位置:首页 > IT技术

hdu2602 骨头收集者 01背包 模板题

时间:2019-06-18 15:43:10来源:IT技术作者:seo实验室小编阅读:70次「手机版」
 

骨头收集者

Bone Collector

Time limit: 2000/1000 MS (java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 75725    Accepted Submission(s): 31393

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 ?

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

15 101 2 3 4 55 4 3 2 1

Sample Output

14

Author

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int v[1005];
int value[1005];
int dp[1005];//dp数组的大小取决于v[i]的最大值 
int main()
{
	int t,v,n;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d%d",&n,&v);
		for(int i=0;i<n;i++)
		scanf("%d",&value[i]);
		for(int i=0;i<n;i++)
		scanf("%d",&v[i]);
		
		memset(dp,0,sizeof(dp));
		
		for(int i=0;i<n;i++)
		{
			for(int j=v;j>=v[i];j--)
			{
				dp[j]=max(dp[j-v[i]]+value[i],dp[j]);
			}
		}	
		printf("%d\n",dp[v]);
	}
	return 0;
}

相关阅读

01背包问题(记忆型的递归)

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

01背包问题,最良心的讲解了

1、动态规划(DP)动态规划(Dynamic Programming,DP)与分治区别在于划分的子问题是有重叠的,解过程中对于重叠的部分只要求解一次,记录下结

【动态规划】01背包问题(通俗易懂,超基础讲解)

问题描述 有n个物品,它们有各自的体积和价值,现有给定容量的背包,如何让背包里装入的物品具有最大的价值总和? 为方便讲解和理解,下面

分享到:

栏目导航

推荐阅读

热门阅读