博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU2602 01背包入门 Bone Collector【01背包模板题】
阅读量:4344 次
发布时间:2019-06-07

本文共 1969 字,大约阅读时间需要 6 分钟。

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

1
5 10
1 2 3 4 5
5 4 3 2 1

Sample Output

14**

题意:容量为V的背包,尽可能装价值更多的石头,输出背包最大的价值

思路:01背包的经典套路,由石头1推到石头n,第i个石头下背包容量V的最大价值由i-1递推而来

#include
#include
#define N 1000int num[N+10],w[N+10],v[N+10];int main(){ int i, j; int t, n, V; scanf("%d",&t); while( t --) { memset(num,0,sizeof(num)); memset(w,0,sizeof(w)); memset(v,0,sizeof(v)); scanf("%d%d",&n,&V); for(i = 1; i <= n; i ++) scanf("%d",&w[i]); for( i = 1; i <= n; i ++) scanf("%d",&v[i]); for( i = 1; i <= n; i ++) for( j = V; j >=v[i];j --)/*j代表背包的剩余容量*/ { num[j] = max(num[j],num[j-v[i]]+w[i]); /*num[j]表示不把第i个石头放入背包的价值*/ /*num[j-v[i]]+w[i]表示把第i个石头放入背包后的价值*/ /*比较二者的最优解存入num[j]*/ } printf("%d\n",num[V]); } return 0;}

转载于:https://www.cnblogs.com/hellocheng/p/7350163.html

你可能感兴趣的文章
java基础之集合:List Set Map的概述以及使用场景
查看>>
Python 线程 进程 协程
查看>>
iOS语言中的KVO机制
查看>>
excel第一次打开报错 向程序发送命令时出错 多种解决办法含终极解决方法
查看>>
响应式web设计之CSS3 Media Queries
查看>>
实验三
查看>>
机器码和字节码
查看>>
环形菜单的实现
查看>>
【解决Chrome浏览器和IE浏览器上传附件兼容的问题 -- Chrome关闭flash后,uploadify插件不可用的解决办法】...
查看>>
34 帧动画
查看>>
二次剩余及欧拉准则
查看>>
thymeleaf 自定义标签
查看>>
关于WordCount的作业
查看>>
UIView的layoutSubviews,initWithFrame,initWithCoder方法
查看>>
STM32+IAP方案 实现网络升级应用固件
查看>>
用74HC165读8个按键状态
查看>>
jpg转bmp(使用libjpeg)
查看>>
linear-gradient常用实现效果
查看>>
sql语言的一大类 DML 数据的操纵语言
查看>>
VMware黑屏解决方法
查看>>