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. 安装debian 9.1后,中文环境下将home目录下文件夹改为对应的英文

    安装了debian 9.1后,中文环境下home目录下文件夹显示的是中文,相当不方便cd命令,改为对应的英文吧,需要用到的软件xdg-user-dirs-gtk #安装需要的软件 sudo apt i ...

  2. kettle系列一之eclipse开发

    1.引言 最近公司开始一个etl项目,底层结合开源的kettle进行开发.那么学习kettle势在必行,kettle的使用在这里就不用介绍了,网上有很多的资料.例如:kettle中文社区,我们在这里主 ...

  3. python第四课——线程、进程、协程

    面试or笔试题:简述线程.进程.协程之间的关系? 内容概要 1.进程与线程优.缺点的比较 2.适用情况 3.线程 线程的创建 setDaemon join event RLock 队列 4.进程 创建 ...

  4. 项目总结二:模块管理之requireJS

    项目开发前期,对究竟用requireJS 还是sea.js 进行讨论,最后采用requireJS,但是后期遇到了问题--当谷歌地图不能加载时,整个页面卡死的状况. requirejs 的作用: 防止j ...

  5. gridContro使用随记

    gridControl设置列宽自动适应内容:绑定数据后调用如下代码即可设置.gridControl1.DataSource = m_pTablegridView1.BestFitColumns(); ...

  6. unity客户端基本框架(转载)

    框架概述: 基础系统的框架搭建,其中包括: UI框架(NGUI + MVC) 消息管理(Advanced CSharp Messenger) 网络层框架(Socket + Protobuf ) 表格数 ...

  7. CentOS6.8通过yum安装MySQL5.7

    Centos6.8通过yum安装mysql5.7 1.安装mysql的yum源 a.下载配置mysql的yum源的rpm包 根据上面3张图片中的操作下载下来的rpm文件可以通过如下命令获取: wget ...

  8. JavaNIO阻塞IO

    package com.java.NIO; import java.io.IOException; import java.net.InetSocketAddress; import java.nio ...

  9. 原生 drag drop HTML5

    drag事件( dragstart -- drag -- dragend )   当按下鼠标开始drag一个可以拖动的对象时,触发dragstart事件,如果元素是不可拖动的话,会出现一个不可拖动的图 ...

  10. ⑩bootstrap组件 导航 使用基础案例

        <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8 ...