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
 
 
题意:给你n个骨头的价值和体积,求体积为V时最多有多少价值;
01背包问题:
递推公式:dp[i][j]=dp[i+1][j];(j<w[i]);
     dp[i][j]=max(dp[i+1][j],dp[i+1][j-w[i]]+v[i]);
代码如下:
 
关于i的逆向循环;
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
using namespace std;
#define N 1100
int dp[N][N];
int main()
{
int T,i,j,V,n,v[N],w[N]; scanf("%d",&T);
while(T--)
{
memset(dp,,sizeof(dp));
scanf("%d%d",&n,&V);
for(i=;i<n;i++)
scanf("%d",&v[i]);
for(i=;i<n;i++)
scanf("%d",&w[i]);
for(i=n-;i>=;i--)
{
for(j=;j<=V;j++)
{
if(j<w[i])
dp[i][j]=dp[i+][j];
else
dp[i][j]=max(dp[i+][j],dp[i+][j-w[i]]+v[i]);
}
}
printf("%d\n",dp[][V]);
}
return ;
}
下面是关于i的正向循环;
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
using namespace std;
#define N 1100
int dp[N][N];
int main()
{
int T,i,j,V,n,v[N],w[N]; scanf("%d",&T);
while(T--)
{
memset(dp,,sizeof(dp));
scanf("%d%d",&n,&V);
for(i=;i<n;i++)
scanf("%d",&v[i]);
for(i=;i<n;i++)
scanf("%d",&w[i]);
for(i=;i<n;i++)
{
for(j=;j<=V;j++)
{
if(j<w[i])
dp[i+][j]=dp[i][j];
else
dp[i+][j]=max(dp[i][j],dp[i][j-w[i]]+v[i]);
}
}
printf("%d\n",dp[n][V]);
}
return ;
}

用一维数组;

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
using namespace std;
#define N 1100
int dp[N];
int main()
{
int T,i,j,V,n,v[N],w[N]; scanf("%d",&T);
while(T--)
{
memset(dp,,sizeof(dp));
scanf("%d%d",&n,&V);
for(i=;i<n;i++)
scanf("%d",&v[i]);
for(i=;i<n;i++)
scanf("%d",&w[i]);
for(i=;i<n;i++)
{
for(j=V;j>=w[i];j--)
{
dp[j]=max(dp[j],dp[j-w[i]]+v[i]);
}
}
printf("%d\n",dp[V]);
}
return ;
}

Bone Collector--hdu2602(01背包)的更多相关文章

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

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

  2. HDU2602 Bone Collector(01背包)

    HDU2602 Bone Collector 01背包模板题 #include<stdio.h> #include<math.h> #include<string.h&g ...

  3. HDU2602 Bone Collector 【01背包】

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

  4. hdu2602 Bone Collector(01背包) 2016-05-24 15:37 57人阅读 评论(0) 收藏

    Bone Collector Problem Description Many years ago , in Teddy's hometown there was a man who was call ...

  5. Hdu2602 Bone Collector (01背包)

    Problem Description Many years ago , in Teddy’s hometown there was a man who was called “Bone Collec ...

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

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

  7. HDU 2639 Bone Collector II(01背包变形【第K大最优解】)

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

  8. hdu 2602 - Bone Collector(01背包)解题报告

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

  9. 题解报告:hdu 2602 Bone Collector(01背包)

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

  10. hdu–2369 Bone Collector II(01背包变形题)

    题意:求解01背包价值的第K优解. 分析: 基本思想是将每个状态都表示成有序队列,将状态转移方程中的max/min转化成有序队列的合并. 首先看01背包求最优解的状态转移方程:\[dp\left[ j ...

随机推荐

  1. Robot Framework配置发送邮件功能

    请参考:http://www.robotframework.net/?/article/118

  2. Linux下安装配置MySQL

    一.删除原来的MySQL 在安装前要先确定系统是否已经安装了其他版本的MySQL,如已安装其他版本的MySQL,需先删除后再安装新版本. 1. 执行yum命令,删除MySQL的lib库,服务文件 yu ...

  3. Maven编译出现“[ERROR] java.lang.OutOfMemoryError: Java heap space”

    Windows下添加环境变量MAVEN_OPTS的value为-Xms1024m -Xmx1024m -Xss1m Linux下可修改.profile或者.bash_profile文件,并做如下设置: ...

  4. XPath的初步认识

    嘿嘿,最近开始上班,不是过于太忙,而是自己一直在学习一些项目中用到的而我暂时还没接触的知识,WCF,log4等,感觉还没有总结的需要吧,虽然都了解啦,但是暂时还是初步的学习,基础的暂时是知道啦,还没有 ...

  5. react中的hoc和修饰器@connect结合使用

    在学习react-redux的时候,看到了修饰器这个新的属性,这个是es7的提案属性,很方便.于是我用@connect代替了connect(使用的时候需要配置,这里不赘述),省去了很多不必要的代码,但 ...

  6. 《C++ Primer Plus》16.1 string类 学习笔记

    16.1.1 构造字符串程序清单16.1使用了string的7个构造函数.程序清单16.1 str1.cpp---------------------------------------------- ...

  7. Python进阶 学习笔记(二)

    (涉及内容:面向对象,类的继承) 定义类并创建实例 在Python中,类通过 class 关键字定义.以 Person 为例,定义一个Person类如下: class Person(object): ...

  8. 常见微信小程序开发工具

    图标: 1.iconfont图标库:http://www.iconfont.cn/home/index?spm=a313x.7781069.1998910419.2

  9. laravel curl post json

    <?php namespace App\BO; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Log; us ...

  10. 记录一次OOM排查经历(一)

    一.经历概要 程序里有个跑数据的job,这个job的主要功能是往数据库写假数据. 既需要跑历史数据(传给job的日期是过去的时间),也需要能够上线后,实时跑(十秒钟触发一次,传入触发时的当前时间). ...