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
思路:
 二维代码:

#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;
long long max(long long num1,long long num2)//比较函数
{
return num1>num2?num1:num2;
}
long long dp[][];//定义背包数组
int main()
{
int t;//数据组数
int m_bone,m_volume;
int bone[], volume[];
cin >> t;
while (t--)
{
int i, j, k;
memset(dp, , sizeof(dp));//初始化
memset(bone,,sizeof(bone));
memset(volume, , sizeof(volume));
cin >> m_bone >> m_volume;//输入骨头总数和容量
for (i = ; i <= m_bone; i++)
cin >> bone[i];//输入价值
for (j = ; j <= m_bone; j++)
cin >> volume[j];//输入容量
for (i = ; i <= m_bone; i++)
{
for (j = ; j <= m_volume; j++)
{
if (j < volume[i])//如果装不下
{
dp[i][j] = dp[i - ][j];//将上一个值赋给当前的价值
continue;
}
else
{
dp[i][j] = max(dp[i - ][j], dp[i - ][j - volume[i]] + bone[i]);//动态转移方程
}
}
}
cout << dp[m_bone][m_volume] << endl;//输出价值最大
}
return ;
优化成:一维代码:
#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;
long long max(long long num1,long long num2)//比较函数
{
return num1>num2?num1:num2;
}
long long dp[];//定义背包数组
int main()
{
int N;
int i,j,k;
int bone[];//定义价值
int volume[];//定义容量
int t;
scanf("%d",&t);
while(t--)
{
memset(dp,,sizeof(dp));//初始化
int count,weight;
scanf("%d%d",&count,&weight);//输入组数和总重量
for(j=;j<=count;j++)
scanf("%d",&bone[j]);
for(j=;j<=count;j++)
scanf("%d",&volume[j]);
for(i=;i<=count;i++)
{
for(k=weight;k>=;k--)
{
if(k-volume[i]<)//装不下
{
break;
}
else
dp[k]=max(dp[k-volume[i]]+bone[i],dp[k]);
cout << dp[k] << endl;
}
}
printf("%lld\n",dp[weight]);
}
return ;
}

Bone Collector-HDU的更多相关文章

  1. bone collector hdu 01背包问题

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

  2. 背包!背包!HDU 2602 Bone Collector + HDU 1114 Piggy-Bank + HDU 2191 512

    http://acm.hdu.edu.cn/showproblem.php?pid=2602 第一题 01背包问题 http://acm.hdu.edu.cn/showproblem.php?pid= ...

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

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

  4. HDU 3639 Bone Collector II(01背包第K优解)

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

  5. HDU 2602 Bone Collector (简单01背包)

    Bone Collector http://acm.hdu.edu.cn/showproblem.php?pid=2602 Problem Description Many years ago , i ...

  6. hdu 2602 Bone Collector 背包入门题

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2602 题目分析:0-1背包  注意dp数组的清空, 二维转化为一维后的公式变化 /*Bone Coll ...

  7. hdu 2639 Bone Collector II

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

  8. HDU 2602 Bone Collector

    http://acm.hdu.edu.cn/showproblem.php?pid=2602 Bone Collector Time Limit: 2000/1000 MS (Java/Others) ...

  9. Bone Collector II(HDU 2639 DP)

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

  10. 【01背包】HDU 2602 Bone Collector (模板题)

    Problem Description Many years ago , in Teddy's hometown there was a man who was called "Bone C ...

随机推荐

  1. 学web前端开发写给新手的建议,超实用!

    01 前面的话 如今我们使用的互联网,客户端与服务器端的交互无时无刻不在发生.比如我们在浏览器打开网页,浏览器就是客户端,将网页数据发过来的也就是服务器.其实服务器,并没有什么特别的,也就是一台昼夜不 ...

  2. javascript倒计时调转页面

    <html><head><meta http-equiv="Content-Type" content="text/html; charse ...

  3. 微信公众号开发(十二)OAuth2.0网页授权

    OAuth允许用户提供一个令牌,而不是用户名和密码来访问它们存放在特定服务器上的数据,每一个令牌授权一个特定的网站在特定时段内访问特定的资源. 授权过程如下: 1.引导用户进入授权页面同意授权,获取c ...

  4. Redhat Linux 自动修改密码

    bash下使用echo+passwd命令修改密码的方法.方法一:echo "Password" |passwd username --stdin方法二:(echo "Pa ...

  5. linux学习(六)绝对路径、相对路径、cd、mkdir、rmdir、rm

    一.绝对路径 就是从根开始的,如:/root./usr/local. 二.相对路径 相对于当前路径的,比如我们在当前路径下建立了一个a.txt. [root@iZ25lzba47vZ ~]# pwd ...

  6. Leetcode题解(21)

    62. Unique Paths 题目 分析: 机器人一共要走m+n-2步,现在举个例子类比,有一个m+n-2位的二进制数,现在要在其中的m位填0,其余各位填1,一共有C(m+n-2,m-1)种可能, ...

  7. Interviewe

    Interviewe Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Su ...

  8. ES6中数组的新方法

    数组的扩展 1.1扩展运算符 1.1.1:... 扩展运算符(spread)是三个点(...).它好比 rest 参数的逆运算,将一个数组转为用逗号分隔的参数序列. <body> < ...

  9. Linux中常见问题(磁盘 定时任务)

    第1章 linux无法上网 1)     第一步,先ping域名. ping www.baidu.com 2)再ping一个公网ip , ping 223.5.5.5/223.6.6.6/114.11 ...

  10. linux dig 命令

    dig 命令主要用来从 DNS 域名服务器查询主机地址信息. 查询单个域名的 DNS 信息 dig 命令最典型的用法就是查询单个主机的信息. $ dig baidu.com dig 命令默认的输出信息 ...