HDU 2602--Bone Collector【01背包】
来源:互联网 发布:淘宝不匿名购买会怎样 编辑:程序博客网 时间:2024/06/11 17:51
Bone Collector
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 34467 Accepted Submission(s): 14191
Total Submission(s): 34467 Accepted Submission(s): 14191
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
如果要求恰好装满背包,那么在初始化时除了dp[0]为0其它dp[1..V]均设为-∞,这样就可以保证最终得到的f[N]是一种恰好装满背包的最优解。
如果并没有要求必须把背包装满,而是只希望价格尽量大,初始化时应该将dp[0..V]全部设为0。
为什么呢?可以这样理解:初始化的f数组事实上就是在没有任何物品可以放入背包时的合法状态。如果要求背包恰好装满,
那么此时只有容量为0的背包可能被价值为0的nothing“恰好装满”,其它容量的背包均没有合法的解,属于未定义的状态,
它们的值就都应该是-∞了。如果背包并非必须被装满,那么任何容量的背包都有一个合法解“什么都不装”,这个解的价值为0,
所以初始时状态的值也就全部为0了。
如果并没有要求必须把背包装满,而是只希望价格尽量大,初始化时应该将dp[0..V]全部设为0。
为什么呢?可以这样理解:初始化的f数组事实上就是在没有任何物品可以放入背包时的合法状态。如果要求背包恰好装满,
那么此时只有容量为0的背包可能被价值为0的nothing“恰好装满”,其它容量的背包均没有合法的解,属于未定义的状态,
它们的值就都应该是-∞了。如果背包并非必须被装满,那么任何容量的背包都有一个合法解“什么都不装”,这个解的价值为0,
所以初始时状态的值也就全部为0了。
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;#define max(a,b) ((a)>(b)?(a):(b))#define maxn 1100int val[maxn],vol[maxn],dp[maxn];int main (){ int t,i,j,n,v; scanf("%d",&t); while(t--){ memset(dp,0,sizeof(dp)); scanf("%d%d",&n,&v); for(i=1;i<=n;++i) scanf("%d",&val[i]); for(i=1;i<=n;++i) scanf("%d",&vol[i]); for(i=1;i<=n;++i) for(j=v;j-vol[i]>=0;j--) dp[j]=max(dp[j-vol[i]]+val[i],dp[j]); printf("%d\n",dp[v]); } return 0;}
0 0
- hdu 2602 (01 背包)Bone Collector
- hdu 2602 Bone Collector 01背包
- hdu 2602 Bone Collector 01背包
- hdu 2602 Bone Collector (01背包)
- hdu 2602 Bone Collector【01背包】
- hdu 2602 Bone Collector 简单01背包
- hdu 2602 Bone Collector(01背包)
- hdu 2602 Bone Collector (01背包)
- HDU 2602 Bone Collector(裸01背包)
- HDU 2602 Bone Collector( 01背包 )
- HDU 2602 Bone Collector(01背包)
- hdu 2602 Bone Collector 01背包
- HDU 2602 Bone collector 01背包
- hdu 2602 Bone Collector (01背包 )
- hdu 2602 Bone Collector (01背包)
- HDU 2602 Bone Collector (01背包)
- HDU--2602 -- Bone Collector [01背包]
- HDU 2602 Bone Collector(01背包)
- 英文序數寫法與唸法 Ordinal Numbers(轉載)
- 高级的设计导航功能
- cocos2d-x的实现类
- java中的集合类
- strace工具的实现原理
- HDU 2602--Bone Collector【01背包】
- C++RTTI小总结
- Play On Words
- [动态规划] 黑客的攻击 Hacker's CrackDown Uva 11825
- JavaScript点滴记录(一)
- 360面试经历回忆录
- 大位图二次采样压缩解决Bitmap OOM
- Java 中的语法糖 (Syntactic Sugar)
- 调用构造函数进行类型转换