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. windows防火墙设置端口开放技巧

    选择“打开或者关闭windows防火墙”把防火墙打开,然后选择“高级设置”,选择“创建规则”来指定端口.(这里也可以在“入站规则”里选择已经存在的端口.) 指定ip开放3389端口 某新服务器,开放8 ...

  2. Libjingle库 综述

    国内现在很多语音聊天工具都是基于TURN方式实现的,包括YY.AK等等,这种方式对于服务器的性能要求很高,而且在用户量增大的时候,服务器压力也会越来越大,用户的语音质量也会受到很大影响.而基于P2P方 ...

  3. Java面试题全集

    Java面试题全集(上) Java面试题全集(中) Java面试题全集(下) http://www.importnew.com/21445.html

  4. ios设备唯一标识获取策略

    In iOS 7 and later, if you ask for the MAC address of an iOS device, the system returns the value 02 ...

  5. 【node.js】】MSBUILD : error MSB3428: 未能加载 Visual C++ 组件“VCBuild.exe”。

    错误原因:缺少windows构建插件 解决方法: # (全局安装windows构建工具) npm install --global --production windows-build-tools

  6. 【SpringCloud微服务实战学习系列】创建应用及解析

    一.创建应用 使用官方Spring Initializr工具生成基础项目(http://start.spring.io/) 导入Intellij idea中 目录结构如下: 二.目录结构说明: src ...

  7. 【大数据系列】hadoop单机模式安装

    一.添加用户和用户组 adduser hadoop 将hadoop用户添加进sudo用户组 sudo usermod -G sudo hadoop 或者 visudo 二.安装jdk 具体操作参考:c ...

  8. 链表一元多项式计算器的实现(Java语言描述)

    链表的经典应用,程序在多项式相加同类项合并还有小的瑕疵,有待改进. 代码如下: package 一元多项式计算器; public class PolyNode { private double a; ...

  9. scanf printf gets() puts(),cin cout

    最近在练机试题,常用的C和C++输入输出如下: 1 scanf 和printf int a; scanf("%d",&a) ; printf("%d", ...

  10. jQuery的回调管理机制

    // 对option的一个缓存,避免每次都需要createOptions,option是创建Callback对象时的传入的参数// 每个option被存入optionsCache中类似于{memory ...