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. hashlib模块--摘要算法

    算法介绍: Python的hashlib提供了常见的摘要算法:MD5,SHA()等. 摘要算法,又称哈希算法,散列算法.通过一个函数,吧任意长度的字符串转换为固定长度的字符串(16进制) 摘要算法就是 ...

  2. 迭代法与开根号求值(letcode 69)

    p { margin-bottom: 0.25cm; line-height: 120% } 一.理论证明 p { margin-bottom: 0.25cm; line-height: 120% } ...

  3. formData 无需form异步上传多个图片

    上周帮其它公司套一下一个web端微信投票系统的后台接口,遇到了一个图片以及视频上传报名的小问题,网上实现方式有很多但都不是ui上面的效果,于是自己动手改造了一个.先来看看效果图 流程很简单,就是点击每 ...

  4. Mysql存在则更新,没有则新增

    insert ignore 当插入数据时,如出现错误时,如重复数据,将不返回错误,只以警告形式返回. insert ignore into table(col1,col2) values ('val1 ...

  5. LeetCode 191. Number of 1 bits (位1的数量)

    Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also know ...

  6. swift之函数式编程(二)

    本文的主要内容来自<Functional Programming in Swift>这本书,有点所谓的观后总结 在本书的Introduction章中: we will try to foc ...

  7. Ionic3 遇到的一些错误-submodule update -q --init --recursive

    解决方法: ionic start myTabs tabs --skip-deps cd .\myTabs cnpm install --save-dev ionic serve > npm i ...

  8. 2015-2016 ACM-ICPC, NEERC, Southern Subregional Contest A Email Aliases(模拟STL vector+map)

    Email AliasesCrawling in process... Crawling failed Time Limit:2000MS     Memory Limit:524288KB     ...

  9. linux进程资源占用高原因分析命令记录

    1.查看进程的线程: ps -eLf|egrep 'gateserver|UID' 2.跟踪线程调用: strace -p 15530 3.统计线程中函数的调用小号CPU时间: strace -p 1 ...

  10. Android 开发笔记___复选框__checkbox

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout ...