hdu 2602 * 01背包 一维数组 不限装满
来源:互联网 发布:好听的中文歌 知乎 编辑:程序博客网 时间:2024/05/29 08:25
Bone Collector
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 9840 Accepted Submission(s): 3778
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
二维数组:
#include <iostream>using namespace std;int opt[1001][1001];int v[1001],w[1001];int main(){ int t,N,i,j,V; cin>>t; while(t--) { cin>>N>>V; for(i=1;i<=N;i++) scanf("%d",&w[i]); for(i=1;i<=N;i++) scanf("%d",&v[i]); memset(opt,0,sizeof(opt)); //初始化 for(i=1;i<=N;i++) for(j=0;j<=V;j++) //注意要从0开始,这个题测试数据有点变态,有的骨头有价值,但占的空间是0 { if(v[i]<=j && opt[i-1][j]<opt[i-1][j-v[i]]+w[i]) opt[i][j]=opt[i-1][j-v[i]]+w[i]; else opt[i][j]=opt[i-1][j]; } cout<<opt[N][V]<<endl; } return 0;}
一维数组:
#include<stdio.h>#include<stdlib.h>#include<string.h>#include<iostream>#define maxV 1111//总价值#define maxN 1111//总量long opt[maxV];long w[maxN],v[maxN];int N,V,t;int main(void){int i,j; scanf("%d",&t); while(t-->0){scanf("%d%d",&N,&V);//V 背包容量for(i=0;i<N;i++){scanf("%ld",v+i); //为什么写成v[i]就wa! 但是换成cin是可以的}for(i=0;i<N;i++){scanf("%ld",w+i);}memset(opt,0,sizeof(opt));//用0初始化 不限定背包装满 即opt[v]不限;for(i=0;i<N;i++)for(j=V;j-w[i]>=0;j--) //j-w[i]>=0 即是一维数组里的if判断 也是ZeroOnePark的优化方案 opt[j] = std::max(opt[j],opt[j-w[i]]+v[i]); printf("%ld\n",opt[V]); }return 0;}
- hdu 2602 * 01背包 一维数组 不限装满
- 知识点:01背包(多种姿势:二维实现+一维实现+滚动数组实现+背包装满+输出最优方案)
- 01背包 装满 hdu 1079 The Balance
- 01背包装满
- (DP,背包,数量不限,装满)Piggy-Bank -- HDOJ
- DP背包问题小结(01背包,完全背包,需恰好装满或不需,一维DP、二维DP)
- hdu 2670 Girl Love Value(刚好装满的01背包)
- HDU 2602 Bone Collector(01二维背包&一维背包&滚动数组优化二维背包的原理 )
- HDU 2844 Coins(多重背包,装满可行性)
- 01背包中是否背包装满问题
- 01背包中是否背包装满问题
- CF19B 01背包(必须装满)
- soj 3172 Fisherman (01背包的装满)
- 01背包(要求恰好装满)
- HDU 1114 又见完全背包问题(必须装满)
- hdu 多重背包(不需装满) 的模板题
- hdu 1114 完全背包模板题(装满的)
- HDU 1149 背包完全装满(求最小)
- jQuery实现图片切换效果
- maven常用方法总结
- [转]windows 下pear安装
- Pear Mail 的安装
- 关于培训的一些感想
- hdu 2602 * 01背包 一维数组 不限装满
- 国行v880刷ICS4.0.3
- jQuery实现省市县三级联动菜单
- Win8新增快捷键
- ubuntu安装chrome报错解决
- 关闭虚拟机virtualbox
- magic square
- 朴素贝叶斯分类器本质上是线性分类器
- Linux 编程经典书籍推荐