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. 如何使Ubuntu Linux12.04 LTS版可以用root用户登陆

    如何使Ubuntu Linux12.04 LTS版可以用root用户登陆 1.  用普通用户登录2.  在终端执行sudo -s,然后输入当前登录的普通用户密码,进入到root用户模式3.  执行ge ...

  2. iOS - keychain 详解及变化

    keychain介绍 iOS keychain 是一个相对独立的空间,保存到keychain钥匙串中的信息不会因为卸载/重装app而丢失, .相对于NSUserDefaults.plist文件保存等一 ...

  3. thinkphp3.2 判断星期几

    后台 $w=date('w'); $week=array( "=>"星期日", "=>"星期一", "=>&qu ...

  4. 决策树归纳算法之C4.5

    前面学习了ID3,知道了有关“熵”以及“信息增益”的概念之后. 今天,来学习一下C4.5.都说C4.5是ID3的改进版,那么,ID3到底哪些地方做的不好?C4.5又是如何改进的呢? 在此,引用一下前人 ...

  5. delphixe10 android操作 打电话,摄像头,定位等

    XE6 不支持JStringToString.StringTojString.StrToJURI:use Androidapi.Helpers //Splash Image Delphi XE5,XE ...

  6. canvas练习 - 七巧板绘制

    用到的方法: 注意点: stokeStyle等样式要在stroke前边 如果最后只有一个stroke或者fill,那么只填充最后一次路径的,之前的也会画出来但是没有填充看不到.所以每次begin+cl ...

  7. vuex - 常用命令学习及用法整理

    https://vuex.vuejs.org/zh-cn/api.html 命令 用途 备注 this.$store 组件中访问store实例 store.state.a 获取在state对象中的数据 ...

  8. 转载:C/C++关于string.h头文件和string类

    学习C语言时,用字符串的函数例如stpcpy().strcat().strcmp()等,要包含头文件string.h 学习C++后,C++有字符串的标准类string,string类也有很多方法,用s ...

  9. 原生js--表单

    阅读了<javascript权威指南>P396-P409. 一.表单和表单元素的选取 1.选取表单(包含name=“address”属性的form表单) document.querySel ...

  10. Redis学习笔记--Redis配置文件redis.conf参数配置详解

    ########################################## 常规 ########################################## daemonize n ...