HDU2602 (0-1背包)

Bone Collector

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


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
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2602
题意:给出n件物品的重量和价值,放进一个容量为v的背包,使背包里的价值最大。
0-1背包的模板题,可以有两种解法。
一维数组:
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int  w[1100],p[1110];
int f[1110];
int main() 
{int t,n,v;scanf("%d",&t);    while(t--){scanf("%d%d",&n,&v);for(int i=1;i<=n;i++)scanf("%d",&w[i]); //输入物品重量 for(int i=1;i<=n;i++)scanf("%d",&p[i]); //输入物品价值       memset(f,0,sizeof(f));for(int i=1;i<=n;i++)  {for(int j=v;j>=w[i];j--) //这个循环保证了放进去的物品重量不会超过背包所能容纳的重量 
            {if(f[j] < f[j-w[i]] + p[i])  // 如果当前所拥有价值  小于 加上这件物品时创造的价值就更新 f[j]= f[j-w[i]] + p[i]; //   f[j] 表示背包重量为 j 时背包里的最大价值,//  所以f[ j - w[i] ] 表示放进这件物品时的状态(因为放进该件物品后容量就减少了)
            }}    printf("%d\n",f[v]);}        return 0;
}

二维数组:

 1  #include<stdio.h>
 2  #include<string.h>
 3  #include<algorithm>
 4  using namespace std;
 5  int w[1100],p[1110];
 6  int f[1110][1110];
 7  int main()
 8  {
 9      int t,n,v;
10      scanf("%d",&t);    
11      while(t--)
12      {
13          scanf("%d%d",&n,&v);        
14          for(int i=1;i<=n;i++)
15          scanf("%d",&p[i]);
16          for(int i=1;i<=n;i++)
17          scanf("%d",&w[i]);        
18          memset(f,0,sizeof(f));                
19          for(int i=1;i<=n;i++)        
20          {
21              for(int j=0;j<=v;j++)
22              {
23                  if(w[i]<=j) // 这件物品的重量小于当前的容量,也就是说放的进背包 
24                  {
25                      // f[i][j] 表示第 i 件物品在背包容量为 j 时的状态,
26                      //所以 f[i-1][j] 表示背包在上一次容量为 j 时候的状态,也就是没放这件物品的时候 
27                      f[i][j]=max(f[i-1][j],f[i-1][j-w[i]]+p[i]);// 比较 没放进去之前 和放进该物品后 的价值,取最大 
28                            
29                  }
30                  else f[i][j]=f[i-1][j];  // 如果不能放进该物品,则取上一次的状态  
31              }        
32          }
33          printf("%d\n",f[n][v]);                
34      }     
35      return 0;
36  }

 

渣渣一枚,如果有什么不对的地方,还请各位大神批评指正~  (^_^)

转载于:https://www.cnblogs.com/ember/p/4701035.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/375674.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

博客3万访问量了……

博客有3万访问量了呢。自从第一次用了赠送的1500的流量券&#xff0c;粉丝了从零突破了&#xff0c;到现在有150个粉丝了。 之前预想的写博客的初衷&#xff0c;也是记录自己的学习过程&#xff0c;毕竟好记忆不如烂笔头&#xff0c;记录下来就是长长久久的&#xff0c;随时可以…

Mint17 一些安装备忘

1&#xff0c;中文输入法&#xff1a; sudo apt-add-repository ppa:fcitx-team/dailybuild-fcitx-master sudo apt-get update sudo apt-get upgrade sudo apt-get remove ^ibus* sudo apt-get remove ^fcitx* sudo apt-get install fcitx fcitx-bin fcitx-config-common fcit…

error: ‘CV_BGR2RGB’ was not declared in this scope

缺少库&#xff0c;添加相应库就行&#xff0c;ubuntu中Qt Create设置如下 LIBS -L/home/mima111/opencv345/lib -lopencv_imgcodecs -lopencv_core -lopencv_highgui -lopencv_imgproc -lopencv_videoio 实际只要添加-lopencv_imgproc就行&#xff0c;CV_BGR2RGB变量存储在该…

Struts学习之手动验证

* 首先要从页面中获取对应的标签name属性的值&#xff0c;在动作类action中声明同名的属性&#xff0c;提供get和set方法 * 要继承ActionSupport类或者实现Validateable接口 * 重写Validateable接口的validate()方法 * 前提是&#xff1a;要保证setUsername()、va…

《启示录-打造用户喜爱的产品》读书小结

2014年大学毕业和研究生入学之间的暑假&#xff0c;我读完了这本书。该书主要内容为介绍产品经理的一些工作经验。分三方面内容系统介绍&#xff1a;人员、流程和产品。第一遍读后&#xff0c;了解了一些产品经理的工作内容&#xff0c;也学习了很多优秀产品经理的理念。转载于…

循环多少次?

循环多少次&#xff1f; Time Limit : 3000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Submission(s) : 17 Accepted Submission(s) : 12 Problem Description我们知道&#xff0c;在编程中&#xff0c;我们时常需要考虑到时间复杂度&#xff0…

关于数据库表的“记录”与“字段”

何谓表的记录&#xff1a; 就是数据库中的一张表中的随便任意一行称之为记录 何谓表的字段&#xff1a; 就是数据库中的一张表中的随便任意一列称之为字段转载于:https://www.cnblogs.com/cyh2009/p/4706021.html

error: use of deleted function

本文案例仅供参考 出错的代码如下&#xff1a; TEST(Test, test1) {TestImpl impl TestImpl(para1, para2);ASSERT_EQ("jkj", impl.func("22", "33", "44")); }实际应该这样&#xff1a; TEST(Test, test1) {TestImpl impl(para1, …

WPF PasswordBox不支持绑定解决方法

PasswordBox的Password属性因为安全原因不支持直接绑定&#xff0c;可以使用依赖属性实现。直接插入代码 public class PasswordBoxHelper{public static readonly DependencyProperty PasswordProperty DependencyProperty.RegisterAttached("Password",typeof(stri…

error: expected unqualified-id before 'public'

Error Coding class A{ }&#xff1b;class B public : A { };Correct Coding class A{ };class B : public A{ };

C# 方法返回值的个数

方法返回值类型总的来说分为值类型&#xff0c;引用类型,Void 有些方法显示的标出返回值 public int Add(int a,int b) {return ab; } 有些方法隐式的返回返回值&#xff0c;我们可以将上面的方法改改&#xff1a; public void Add(int a,int b,out int sum) {sumab; } 怎么样&…

【Java】Java里String 的equals和==

Java里面有对象和对象的引用的概念&#xff0c;在String方面&#xff0c;比较的是引用&#xff0c;equals比较的是对象的具体值。 String s1 new String("abc");String s2 new String("abc");System.out.println(s1 s2);System.out.println(s1.equals(s…

marked override, but does not override

检查这个函数对应的基类函数 看是否是虚函数看函数参数是否对应

STL中的lower_bound和upper_bound的理解

STL迭代器表述范围的时候&#xff0c;习惯用[a, b)&#xff0c;所以lower_bound表示的是第一个不小于给定元素的位置 upper_bound表示的是第一个大于给定元素的位置。 譬如&#xff0c;值val在容器内的时候&#xff0c;从lower_bound到 upper_bound表示的就是整个容器中与val相…

because the following virtual functions are pure within

构建了包含纯虚函数的对象 包含纯虚函数的类不能实例化为对象

C ~ 链式队列与循环队列

此处的链式与循环队列可以应用于BFS和树的层序遍历。下面是对其结构和基本操作的程序描述。 1、循环队列 解决循环队列的队空和队满的方法&#xff1a; [1].增加一个参数count&#xff0c;用来记录数组中当前元素的个数&#xff1b; [2].为避免队空和满两状态混淆&#xff0c;少…

Hexo之部署github

最近开始学NodeJs&#xff0c;准备也在github上弄个一个Hexo博客练练过程中遇到一些问题总结一下。希望对遇到同样问题的同学能有个帮助少走一些弯路。 - 其实用windows或mac客户端直接去同步很顺利没遇到什么问题。主要是在使用Hexo deploy命令的时候。 我的配置文件deploy部分…

You have unstaged changes.

执行git rebase出错 原因&#xff1a;有未提交的修改 解决&#xff1a;执行git stash暂时保存修改&#xff0c;rebase后&#xff0c;执行git stash pop

[bootstrap] 打造一个简单的系统模板(1) 左侧折叠菜单

1. 前言 最近需要做一个后台管理系统&#xff0c;我打算使用bootstrap弄一个好看的后台模板。网上的好多模板我觉的css和js有点重。 于是就打算完全依靠bootstrap搭建一个属于自己的模板。 首先从左侧的折叠菜单开始。看图。 2. CSS 代码 以下是自定义的css代码&#xff0c;由于…

将gcc/g++链接到指定版本

安装指定版本&#xff1a; sudo apt-get install gcc-4.8 sudo apt-get install g-4.8以上命令默认安装的为4.8.5版本&#xff0c;支持c11 建立软连接 cd /usr/bin如果已经装有gcc或者g&#xff0c;需要先移除原先的软连接sudo rm gcc sudo rm g建立新的软连接 sudo ln -s g…