HDU2602 Bone Collector(01背包)
来源:互联网 发布:c语言文件分隔符 编辑:程序博客网 时间:2024/06/01 13:52
题目:
Bone Collector
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 53983 Accepted Submission(s): 22616
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 ?
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.
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
Teddy
Source
HDU 1st “Vegetable-Birds Cup” Programming Open Contest
Recommend
lcy | We have carefully selected several similar problems for you: 1203 2159 2955 1171 2191
题意:意思是一个人有收集骨头的习惯,现在他有一个背包,面前有不同的骨头种类和各自对应的价值,求这个背包能装的最大价值,这些东西不可分割,所以不能用贪心,也就是01背包问题
代码一(一维):
#include <stdio.h>#include <string.h>#include <algorithm>#define mem(a,b) memset(a,b,sizeof(a));using namespace std;int main(){ int t; scanf("%d",&t); while(t--) { int n,v;//个数 和 容量 scanf("%d %d",&n,&v); int a[1010],b[1010]; for(int i=0; i<n; i++) scanf("%d",&a[i]);//每一个的价值 for(int i=0; i<n; i++) scanf("%d",&b[i]);//每一个的体积 int dp[1010];//定义状态 mem(dp,0); for(int i=0; i<n; i++) { for(int j=v; j>=b[i]; j--)//逆序 dp[j]=max(dp[j],dp[j-b[i]]+a[i]); } printf("%d\n",dp[v]); } return 0;}//dp[i][v]是由dp[i-1][v]和dp[i-1][v-c[i]]两个子问题递推而来,能否保证在推dp[i][v]时(也即在第i次主循环中推dp[v]时)能够得到dp[i-1][v]和dp[i-1][v-c[i]]的值呢?事实上,这要求在每次主循环中我们以v=V..0的顺序推dp[v],这样才能保证推dp[v]时dp[v-c[i]]保存的是状态dp[i-1][v-c[i]]的值。伪代码如下:// for i=1..N// for v=V..0// dp[v]=max{dp[v],dp[v-c[i]]+w[i]};//注意:这种解法只能由V--0,不能反过来,如果反过来就会造成物品重复放置!代码二(二维):
#include <stdio.h>#include <string.h>#include <algorithm>#define mem(a,b) memset(a,b,sizeof(a));using namespace std;int dp[1000][1000];//定义状态int main(){ int t; scanf("%d",&t); while(t--) { int n,v;//个数 和 容量 scanf("%d %d",&n,&v); int a[1010],b[1010]; for(int i=1; i<=n; i++) scanf("%d",&a[i]);//每一个的价值 for(int i=1; i<=n; i++) scanf("%d",&b[i]);//每一个的体积 mem(dp,0); for(int i=1; i<=n; i++)//遍历个数 { for(int j=0; j<=v; j++)//遍历背包容量 { if(b[i]<=j)//可以放进去 dp[i][j]=max(dp[i-1][j],dp[i-1][j-b[i]]+a[i]);//第i个物品放入后,那么前i-1个物品可能会放入也可能因为剩余空间不够无法放入 else//不能放进去 dp[i][j]=dp[i-1][j]; } } printf("%d\n",dp[n][v]); } return 0;}
ps:01背包问题,这种背包特点是:每种物品仅有一件,可以选择放或不放。
用子问题定义状态:即dp[i][v]表示前i件物品恰放入一个容量为v的背包可以获得的最大价值。
则其状态转移方程便是:
dp[i][v]=max{dp[i-1][v],dp[i-1][v-cost[i]]+value[i]}
另NYOJ289 苹果和这道题很相似
0 0
- Bone Collector hdu2602 01背包
- HDU2602:Bone Collector(01背包)
- hdu2602 bone collector 01背包
- hdu2602 Bone Collector (01背包)
- hdu2602 Bone Collector(01背包)
- HDU2602 Bone Collector 【01背包】
- HDU2602:Bone Collector(01背包)
- hdu2602 01背包Bone Collector
- hdu2602 Bone Collector--01背包
- HDU2602 Bone Collector(01背包)
- hdu2602 Bone Collector(背包)
- hdu2602 Bone Collector(01背包)
- hdu2602 Bone Collector (01背包)
- hdu2602-Bone Collector(01背包模版题)
- HDU2602-Bone Collector-01背包(模板题)
- hdu2602 Bone Collector (01背包)
- hdu2602 Bone Collector(01背包+入门)
- 【HDU2602】Bone Collector(01背包入门级)
- android ---java.lang.RuntimeException: Parcel: unable to marshal value
- 事务控制概述
- Android 百度地图简单使用
- 第十二周oj题目将度分秒转换为弧度
- 深刻理解getLocationInWindow 和 getLocationOnScreen区别
- HDU2602 Bone Collector(01背包)
- jQuery回调、递延对象总结(中篇) —— 神奇的then方法
- mmap详解
- Git命令
- 点击小图查看大图jQuery插件FancyBox魔幻灯箱
- leetcode_232. Power of Two分析
- c++常见面试题30道
- Android64K方法编译限制
- flume-ng配置文件详解(三)