Bone Collector------HDOJ杭电2602(纯01背包问题!!!!!!具体解释!)
Posted yxwkaifa
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Bone Collector------HDOJ杭电2602(纯01背包问题!!!!!!具体解释!)相关的知识,希望对你有一定的参考价值。
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
1 5 10 1 2 3 4 5 5 4 3 2 1
Sample Output
14
先输入一个t,代表有t个样例。然后输入n和v,n代表有多少骨头。v代表背包体积,每样东西仅仅有一个,也就是说仅仅能取一次,刚開始看这道题的时候全然不知道怎么做。感觉会非常麻烦的样子,学长是作为例题给我们讲的。刚開始有点头绪,但详细还是全然不懂。后来自己专门刷这方面的专题。还算理解的比較好。动态规划的思想得要理解好,这题是用空间换时间。过程中会产生大量之间数据,嗯,就是这样。好了,回到这道题。我们用一维数组来存储数据,可是有些pdf文档比方背包九讲就是用二维数组来解说,都能够的。
如今进入正题:输入前面讲过了,如今讲核心代码
for(i=1;i<=n;i++) for(j=v;j>=c[i];j--) liu[j]=max(liu[j],liu[j-c[i]]+w[i]);
这三行就是核心。就像背包九讲里面说的,这个状态转移方程很重要,一定要理解,它联系了上一个状态和这一个状态,所以叫做状态转移方程!
!!!
!!
为了更加清楚描写叙述执行过程中数组每一个值的详细变化,我在这里加了几行代码:
for(i=1;i<=n;i++) { for(j=v;j>=c[i];j--) { liu[j]=max(liu[j],liu[j-c[i]]+w[i]); } for(k=1;k<=v;k++) printf("%d ",liu[k]); printf("\n"); }
我们以题目给的数据为例,执行结果例如以下:
从图中我们能够看出(结合上面的代码),程序循环五次,每次循环的结果都在动态变化。假设还不能理解,建议自己在草稿子上模拟i=1。i=2的时候数组变化的情况,就会非常好理解了。动态规划给我的感觉就是代码非常短,可是理解之后就非常easy了!!!
!!!
好了。讲完了,近期在做dp专题,会不定时更新做题的心得还有题解,大家一起学习。
以下贴下AC代码:
#include<stdio.h> #include<string.h> #include<math.h> #include<iostream> using namespace std; int main() { int i,j,k; int t,n,m,v; int liu[1006],c[1006],w[1006]; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&v); for(i=1;i<=n;i++) scanf("%d",&w[i]); for(i=1;i<=n;i++) scanf("%d",&c[i]); memset(liu,0,sizeof(liu)); for(i=1;i<=n;i++) { for(j=v;j>=c[i];j--) { liu[j]=max(liu[j],liu[j-c[i]]+w[i]); } for(k=1;k<=v;k++) printf("%d ",liu[k]); printf("\n"); } printf("%d\n",liu[v]); } return 0; }
写代码能力有限,如有编程爱好者发现bug,还请指出,不胜感激!
以上是关于Bone Collector------HDOJ杭电2602(纯01背包问题!!!!!!具体解释!)的主要内容,如果未能解决你的问题,请参考以下文章