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. Python读取SQLite文件数据

    近日在做项目时,意外听说有一种SQLite的数据库,相比自己之前使用的SQL Service甚是轻便,在对数据完整性.并发性要求不高的场景下可以尝试! 1.SQLite简介: SQLite是一个进程内 ...

  2. Visual Studio中让一个JS文件智能提示另一个JS文件中的成员

    当一个Web页面引用了两个JS文件(假如分别叫common.js和JScript1.js),如果JScript1.js中需要调用大量的common.js中的方法,这时候在JScript1.js中智能提 ...

  3. 有序链表--Java实现

    /*有序链表--使用的是单链表实现 *在插入的时候保持按照值顺序排列 *对于删除最小值的节点效率最高--适合频繁的删除最小的节点 * */ public class MySortedLinkList ...

  4. LeetCode 238. Product of Array Except Self (去除自己的数组之积)

    Given an array of n integers where n > 1, nums, return an array output such that output[i] is equ ...

  5. angular 1.5.3各种模块使用(一)

    1.angular cookie的用法:(1)引入angular-cookies(2)注入ngCookies这模块(3)想要更改cookies存储位置的话要添加内置服务$cookiesProvider ...

  6. WCF 内置跟踪日志

    Web.config 配置文件修改: <system.serviceModel> <diagnostics> <messageLogging logEntireMessa ...

  7. Building roads

    Building roads Time Limit: 10000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...

  8. The Worm Turns

    The Worm Turns Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...

  9. linux中必会的目录

    第1章 find命令扩展 1.1 方法一 |xargs 通过|xargs将前面命令的执行结果传给后面. [root@znix ~]# find /oldboy/ -type f -name " ...

  10. zookeeper详解

    ZooKeeper 1.Zookeeper(***必须掌握***) 官方网址:http://zookeeper.apache.org/ Ø 什么是Zookeeper? l  Zookeeper 是 G ...