HDOJ 2602 Bone Collector--01背包

题目来源:HPU 2602--Bone Collector

Problem Description

Manyyears ago , in Teddy’s hometown there was a man who was called “BoneCollector”. 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 ofcollecting there are a lot of bones , obviously , different bone has differentvalue 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

Thefirst line contain a integer T , the number of cases.

Followed by T cases , each case three lines , the first line contain twointeger N , V, (N <= 1000 , V <= 1000 )representing the number of bonesand the volume of his bag. And the second line contain N integers representingthe value of each bone. The third line
contain N integers representing thevolume of each bone.

Output

Oneinteger per line representing the maximum of the total value (this number willbe less than 231).

Sample Input

1

5 10

1 2 3 4 5

5 4 3 2 1

Sample Output

14

考察点:动态规划—01背包

题目大意:

给定T组测试数据,每组数据给定一个n(n块骨头)和一个V(背包的容量V),接下来给定两行分别输入n块骨头的价值和体积。输出背包能装下的最大价值。

题目解析:

动态规划方程:f(n,m)=max{f(n-1,m),f(n-1,m-w[n])+v(n,m)},根据此递推式可知01背包记录的是当前位置的最优解。

AC代码:

#include<cstdio>
#include<cstring>
#include<cstring>
#include<algorithm>
using namespace std;
int value[1005],volume[1005];//骨头价值,体积
int dp[1005][1005];//记录背包体积从0到V所装价值最大值
int main()
{
	int T;
	int n,V;
	scanf("%d",&T);
	while(T--)
	{
		scanf("%d%d",&n,&V);
		memset(dp,0,sizeof(dp));//dp数组要初始化为0
		for(int i=1;i<=n;i++)
		{
			scanf("%d",&value[i]);
		}
		for(int i=1;i<=n;i++)
		{
			scanf("%d",&volume[i]);
		}
		for(int i=1;i<=n;i++)
		{
			for(int j=0;j<=V;j++)
			{
				if(j<volume[i])//当前背包体积j不能装下体积为volume[i]的骨头
				{
					dp[i][j]=dp[i-1][j];
				}
				else//当前背包体积j能装下体积为volume[i]的骨头
				{
					//选择加或不加当前背包的价值
					dp[i][j]=max(dp[i-1][j],dp[i-1][j-volume[i]]+value[i]);
				}
			}
		}
		printf("%d\n",dp[n][V]);
	}
	return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-05 04:55:54

HDOJ 2602 Bone Collector--01背包的相关文章

HDU 2602 Bone Collector(01背包裸题)

Bone Collector Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 60469    Accepted Submission(s): 25209 Problem Description Many years ago , in Teddy’s hometown there was a man who was called “Bo

hdu 2602 Bone Collector 01背包

Bone Collector Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 50442    Accepted Submission(s): 21153 Problem Description Many years ago , in Teddy’s hometown there was a man who was called “Bon

HDU 2602 Bone Collector (01背包DP)

题意:给定一个体积,和一些物品的价值和体积,问你最大的价值. 析:最基础的01背包,dp[i] 表示体积 i 时最大价值. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <iostream>

hdoj 2620 Bone Collector(0-1背包)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2602 思路分析:该问题为经典的0-1背包问题:假设状态dp[i][v]表示前i件物品恰放入一个容量为v的背包可以获得的最大价值,则可以推导出dp递推公式 dp[i][v] = Max{dp[i-1][v], dp[i-1][v – c[i]] + w[i]}:c[i]表示第i件物品的容量,w[i]表示第i件物品的价值:该动态规划问题每个阶段的决策为是否要 选择第i件物品放入背包中,如果不选择第i件物

hdu 2602 Bone Collector 01背包模板

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2602 题目要求是尽量装的最多,所以初始化的时候都为0即可. 如果要求恰好装满,初始化的时候除了dp[0] = 0,其他都要设成-inf,表示不合法情况. 1 #include <bits/stdc++.h> 2 using namespace std; 3 int T; 4 int dp[1010][1010]; 5 int v[1010], w[1010]; 6 int main() 7 { 8

hdu2602 Bone Collector (01背包)

本文出自:http://blog.csdn.net/svitter 题意:典型到不能再典型的01背包.给了我一遍AC的快感. //============================================================================ // Name : 2602.cpp // Author : vit // Version : // Copyright : Your copyright notice // Description : Hello

HDOJ 2602 Bone Collector【01背包】

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2602 Bone Collector Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 34251    Accepted Submission(s): 14101 Problem Description Many years ago , in

hdoj 2602 Bone Collector 【01背包】

意甲冠军:给出的数量和袋骨骼的数,然后给每块骨骼的价格值和音量.寻求袋最多可容纳骨骼价格值 难度;这个问题是最基本的01背包称号,不知道的话,推荐看<背包9说话> AC by SWS 主题链接 http://acm.hdu.edu.cn/showproblem.php?pid=2602 代码: #include<stdio.h> #include<string.h> typedef struct{ int w, v; }str; str s[1005]; int dp[

hdoj 2602 Bone Collector【0-1背包】【dp思想】

Bone Collector Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 40247    Accepted Submission(s): 16722 Problem Description Many years ago , in Teddy's hometown there was a man who was called "Bo

HDU 2602 Bone Collector (01背包问题)

原题代号:HDU 2602 原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=2602 原题描述: 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 ,