原题代号:HDU 2602
原题描述:
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的袋子,n个骨头,给出每个骨头的价值和体积。问最大能收集的价值是多少。(01背包问题)
 
解法一:时间,空间复杂度均为O(n2)

  1. # include <stdio.h>
  2. # include <string.h>
  3. # include <stdlib.h>
  4. # include <iostream>
  5. # include <fstream>
  6. # include <vector>
  7. # include <queue>
  8. # include <stack>
  9. # include <map>
  10. # include <math.h>
  11. # include <algorithm>
  12. using namespace std;
  13. # define pi acos(-1.0)
  14. # define mem(a,b) memset(a,b,sizeof(a))
  15. # define FOR(i,a,n) for(int i=a; i<=n; ++i)
  16. # define For(i,n,a) for(int i=n; i>=a; --i)
  17. # define FO(i,a,n) for(int i=a; i<n; ++i)
  18. # define Fo(i,n,a) for(int i=n; i>a ;--i)
  19. typedef long long LL;
  20. typedef unsigned long long ULL;
  21.  
  22. int a[+],b[+],dp[+][+];
  23.  
  24. int main()
  25. {
  26. int t,n,v;
  27. cin>>t;
  28. while(t--)
  29. {
  30. cin>>n>>v;
  31. FOR(i,,n)cin>>b[i];
  32. FOR(i,,n)cin>>a[i];
  33. mem(dp,);
  34. FOR(i,,n)FOR(j,,v)
  35. {
  36. if(a[i]<=j)dp[i][j]=max(dp[i-][j],dp[i-][j-a[i]]+b[i]);
  37. else dp[i][j]=dp[i-][j];
  38. }
  39. cout<<dp[n][v]<<endl;
  40. }
  41. return ;
  42. }

解法二:时间复杂度O(n2),空间复杂度O(n)

  1. # include <stdio.h>
  2. # include <string.h>
  3. # include <stdlib.h>
  4. # include <iostream>
  5. # include <fstream>
  6. # include <vector>
  7. # include <queue>
  8. # include <stack>
  9. # include <map>
  10. # include <math.h>
  11. # include <algorithm>
  12. using namespace std;
  13. # define pi acos(-1.0)
  14. # define mem(a,b) memset(a,b,sizeof(a))
  15. # define FOR(i,a,n) for(int i=a; i<=n; ++i)
  16. # define For(i,n,a) for(int i=n; i>=a; --i)
  17. # define FO(i,a,n) for(int i=a; i<n; ++i)
  18. # define Fo(i,n,a) for(int i=n; i>a ;--i)
  19. typedef long long LL;
  20. typedef unsigned long long ULL;
  21.  
  22. int a[+],b[+],dp[+];
  23.  
  24. int main()
  25. {
  26. int t,n,v;
  27. cin>>t;
  28. while(t--)
  29. {
  30. int ans=;
  31. cin>>n>>v;
  32. FOR(i,,n)cin>>b[i];
  33. FOR(i,,n)cin>>a[i];
  34. mem(dp,);
  35. FOR(i,,n)For(j,v,)
  36. {
  37. if(a[i]<=j)dp[j]=max(dp[j],dp[j-a[i]]+b[i]);
  38. }
  39. cout<<dp[v]<<endl;
  40. }
  41. return ;
  42. }

HDU 2602 Bone Collector (01背包问题)的更多相关文章

  1. HDU 2602 Bone Collector(01背包裸题)

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

  2. HDU 2602 - Bone Collector - [01背包模板题]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2602 Many years ago , in Teddy’s hometown there was a ...

  3. [HDU 2602]Bone Collector ( 0-1背包水题 )

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2602 水题啊水题 还给我WA了好多次 因为我在j<w[i]的时候状态没有下传.. #includ ...

  4. HDU 2602 Bone Collector --01背包

    这种01背包的裸题,本来是不想写解题报告的.但是鉴于还没写过背包的解题报告.于是来一发. 这个真的是裸的01背包. 代码: #include <iostream> #include < ...

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

    题意:给定一个体积,和一些物品的价值和体积,问你最大的价值. 析:最基础的01背包,dp[i] 表示体积 i 时最大价值. 代码如下: #pragma comment(linker, "/S ...

  6. HDOJ(HDU).2602 Bone Collector (DP 01背包)

    HDOJ(HDU).2602 Bone Collector (DP 01背包) 题意分析 01背包的裸题 #include <iostream> #include <cstdio&g ...

  7. HDU 2602 Bone Collector 0/1背包

    题目链接:pid=2602">HDU 2602 Bone Collector Bone Collector Time Limit: 2000/1000 MS (Java/Others) ...

  8. HDU 2602 Bone Collector(经典01背包问题)

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

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

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

随机推荐

  1. Python3 字符编码到底是个什么鬼

    首先ASCII码是美国人自己给自己用的,只针对英文及一系列符号,凭想象预留了编码位置,不料有个东方大国文字过于复杂,预留根本不够,所以这个大国重新搞了个编码gb2312.gbk等,结果就是全世界各国都 ...

  2. 决解nginx代理的django项目的admin站点无法访问,和没样式的问题。

    首先我们先解决无法访问admin站点的问题 首先我们先修改一下nginx的配置,添加红色框框的部分. 然后重新启动一下nginx 访问一下admin站点 发现没有样式了. 我们先修改/fast_foo ...

  3. Java基础语法-运算符

    1算术运算符 1.1运算符和表达式 运算符:对常量和变量进行操作的符号. 表达式:用运算符把常量或者变量连接起来符合java语法的式子就可以称为表达式. 不同运算符链接的表达式体现的是不同类型的表达式 ...

  4. javascript中的继承-寄生组合式继承

    前文说过,组合继承是javascript最常用的继承模式,不过,它也有自己的不足:组合继承无论在什么情况下,都会调用两次父类构造函数,一次是在创建子类原型的时候,另一次是在子类构造函数内部.子类最终会 ...

  5. linux-memcache安装及memcached memcache扩展

    linux memcached安装yum -y install libevent libevent-deve yum list memcached yum -y install memcached m ...

  6. 使用 VS Code 搭建 TypeScript 开发环境

    使用 VS Code 搭建 TypeScript 开发环境 TypeScript 是 JavaScript 的超集,TypeScript 只是增强了 JavaScript 而非改变了 JavaScri ...

  7. Axiso解决跨域访问

    问题: 在项目中需要需要讲本地项目去请求一个URL接口获取数据 例如: 本地请求地址:http://127.0.0.1:19323/site/info.json 请求Url地址:http://www. ...

  8. docker配置Nginx

    创建及进入docker容器$docker run -p 80 --name web01 -i -t ubuntu /bin/bash 创建html文件夹$mkdir -p /var/www/html ...

  9. 前端播放m3u8格式视频

    一.前端播放m3u8格式视频 <!DOCTYPE html> <html lang="zh-CN"> <head> <meta chars ...

  10. Java并发(具体实例)——几个例子

    一步步优化页面渲染功能                                                           本节将模拟一个简单的页面渲染功能,它的作用是将HTML页面绘 ...