Bone Collector

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 85530    Accepted Submission(s): 35381

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
Author
Teddy
Source
HDU 1st “Vegetable-Birds Cup” Programming Open Contest
代码(一维数组):
 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int N=1e5+10;
 4 int val[N],wei[N],dp[N];
 5 int main(){
 6     int t,n,m;
 7     scanf("%d",&t);
 8     while(t--){
 9             memset(val,0,sizeof(val));
10             memset(wei,0,sizeof(wei));
11             memset(dp,0,sizeof(dp));
12         scanf("%d%d",&n,&m);
13         for(int i=0;i<n;i++)
14             scanf("%d",&val[i]);
15         for(int i=0;i<n;i++)
16             scanf("%d",&wei[i]);
17         for(int i=0;i<n;i++){
18             for(int j=m;j>=wei[i];j--){
19                 dp[j]=max(dp[j],dp[j-wei[i]]+val[i]);
20             }
21         }
22         printf("%d\n",dp[m]);
23     }
24     return 0;
25 }

代码(二维数组):

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int N=1e3+10;
 4 int val[N],wei[N],dp[N][N];
 5 int main(){
 6     int t,n,m;
 7     scanf("%d",&t);
 8     while(t--){
 9         memset(dp,0,sizeof(dp));
10         scanf("%d%d",&n,&m);
11         for(int i=1;i<=n;i++)
12             scanf("%d",&val[i]);
13         for(int i=1;i<=n;i++)
14             scanf("%d",&wei[i]);
15         for(int i=1;i<=n;i++){
16             for(int j=0;j<=m;j++){
17                 if(wei[i]<=j)dp[i][j]=max(dp[i-1][j],dp[i-1][j-wei[i]]+val[i]);
18                 else dp[i][j]=dp[i-1][j];
19             }
20         }
21         printf("%d\n",dp[n][m]);
22     }
23     return 0;
24 }

转载于:https://www.cnblogs.com/ZERO-/p/9741042.html

HDU 2602.Bone Collector-动态规划0-1背包相关推荐

  1. hdu 2602 Bone Collector(01背包)模板

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2602 Bone Collector Time Limit: 2000/1000 MS (Java/Ot ...

  2. HDU 2602 Bone Collector DP(01背包)

    Bone Collector Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Desc ...

  3. hdu 2602 Bone Collector 01背包

    Bone Collector Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  4. HDU2602 Bone Collector【0/1背包+DP】

    Bone Collector Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  5. HDU 2602 Bone Collector - from lanshui_Yang

           题目大意:有n件物品,每件物品均有各自的价值和体积,给你一个容量为 V 的背包,问这个背包最多能装的物品的价值是多少?        解题思路:这是一道0 - 1 背包的简单模板题,也是 ...

  6. hdu 2602 Bone Collector(01背包)

    题意:给出包裹的大小v,然后给出n块骨头的价值value和体积volume,求出一路下来包裹可以携带骨头最大价值 思路:01背包 1.二维数组(不常用 #include<iostream> ...

  7. hdu 2602 Bone Collector 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2602 在没学01背包时做的,很遗憾的是,wa了很多次. wa代码 1 #include <ios ...

  8. hdu 2602 Bone Collector

    终于开始做自己一直不敢碰的dp了,内心颇不平静.很久以前看过的背包九讲也没什么印象了.这个题错了四次.之前老师有说过看到dp不要把它想成dp,要按照数学归纳的路子来.先来个最初的归纳假设,如果过弱再加 ...

  9. HDU 2639 Bone Collector II(01背包-第K优决策)

    Description 给出n件物品的价值和体积,问在总体积不超过v时的第k大价值 Input 第一行为用例组数T,每组用例第一行为三个整数n,v和k,第二行n个整数表示这n件物品的价值,第三行n个整 ...

最新文章

  1. QButtonGroup 的使用
  2. 手写一个简单的HashMap,搞定挑剔面试官
  3. Android性能优化——使用 APK Analyzer 分析你的 APK
  4. Windows实用快捷键
  5. MySQL 覆盖索引、最左前缀原则、索引下推
  6. 周鸿祎:比情怀更重要的硬件创业三定律
  7. 必看总结!深度学习时代您应该阅读的10篇文章了解图像分类!
  8. 订单金额等字段设置decimal时,要禁止为负数
  9. linux系统管理命令使用,Linux系统管理命令使用说明
  10. 极客技术专题【002期】:开发小技巧 - 如何使用jQuery来处理图片坏链?
  11. iOS 技术支持网址:
  12. jquery实现回到顶部和回到底部
  13. 九、JavaScript网页特效 - 章节课后练习题及答案
  14. armv8 boot流程(二):软件如何判断当前是cold reset/warm reset/primary boot/senondary boot
  15. 设计师:设计师知识储备(硬装、软装、榻榻米、马卡龙、地台、公共空间、玄关、闭水实验)之详细攻略
  16. 吴恩达访谈 - Geoffrey Hinton(个人不懂或是想了解的名词)
  17. pytest报错 E ModuleNotFoundError解决办法
  18. css像素px,物理像素(pt),设备像素比(dpr),1px边框问题,viewport自适应
  19. 【文献阅读笔记】BoT-SORT: Robust Associations Multi-Pedestrian Tracking
  20. 【泰迪杯-数据分析-1】matplotlib

热门文章

  1. React Native的键盘遮挡问题(input/webview里)
  2. redis aof持久化遇到的Can't open the append-only file Permissi
  3. 利用 %20 替换 空格
  4. 转-Android Studio *.jar 与 *.aar 的生成与*.aar导入项目方法
  5. 标准错误处理机制——error
  6. 用apache的httpclient发请求和接受数据
  7. LeapFTP 注册码
  8. 宝塔 没有找到站点_宝塔面板正确开启TLS 1.3并删除TLS 1.1
  9. MATLAB判断奇偶数
  10. html中给文章怎么设置行高,css如何设置行距?