HDU 2602
来源:互联网 发布:五笔教材 知乎 编辑:程序博客网 时间:2024/06/05 23:04
Bone Collector
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 59572 Accepted Submission(s): 24863
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
01背包:一件物品
5 件物品
编号i 1 2 3 4 5
价值w 1 2 3 4 5
体积c 5 4 3 2 1
背包大小v
物品个数n
列表内容 dp[i][j] = max(dp[i-1][j], dp[i-1][j - c[i]] + w[i]);
dp[i - 1][j - c[i]]:背包未放物品i体积 - 物品i体积
j -> v ; j >= c[i]:这之间都可以放物品i的
for i->1 i <= n i++
for j->1 j<=v j++
dp[i][j] = dp[i-1][j];
if (j >= c[i])
dp[i][j] = max(dp[i-1][j], dp[i-1][j - c[i]] + w[i]);
因为只和前面一个有关用一维数组也行
/*二维伪代码for i->1 i <= n i++ for j->1 j<=v j++ dp[i][j] = dp[i-1][j]; if (j >= c[i]) dp[i][j] = max(dp[i-1][j], dp[i-1][j - c[i]] + w[i]);因为只和前面一个有关用一维数组也行*/#include <iostream>#include <algorithm>#include <cstdio>#include <cmath>#include <cstring>using namespace std;int main(int argc, char *argv[]){ int T,n,v,values[1009],volume[1009],dp[1009]; cin >> T; while (T--) { memset(dp, 0, sizeof(dp)); cin >> n >> v; for (int i = 1; i <= n; i++) { cin >> values[i]; } for (int i = 1; i <= n; i++) { cin >> volume[i]; } for (int i = 1; i <= n; i++) { for (int j = v; j >= volume[i]; j--) { dp[j] = max(dp[j], dp[j - volume[i]] + values[i]); } } cout << dp[v] << endl; } return 0;}
- HDU 2602
- HDU 2602
- HDU 2602
- HDU 2602
- hdu 2602
- hdu 2602
- hdu 2602
- HDU 2602
- HDU 2602
- hdu 2602
- HDU 2602
- hdu 2602
- HDU 2602
- HDU 2602
- HDU 2602
- HDU 2602
- hdu 2602
- HDU 2602
- grep用法
- Java基础的思维导图
- C++学习笔记(20161114 ) 根据指定目标时间获得时间戳(秒数)
- 沙盒防泄密
- oracle中lag()函数和lead()函数的用法
- HDU 2602
- android-获取网络时间、获取特定时区时间、时间同步的方法
- 遍历和二分法求解二维数组中查找问题
- 关于重定向的几个注意点
- 正则表达式总结归纳
- js中的__proto__和prototype
- 关于linker command failed with exit code 1 造成的失败问题的汇总
- WebView与JS的那些事:入门篇
- 网络通讯框架