hdu 2602 Bone Collector 01背包
March 28, 2013
Hdu
DP
Bone Collector
Time Limit: 2000⁄1000 MS (Java/Others) Memory Limit: 32768⁄32768 K (Java/Others) Total Submission(s): 17590 Accepted Submission(s): 6954
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背包,也不知道为什么,好久不做题了,似乎什么也不会,01背包的一维的优化怎么也看不懂,今天看了一个博客,画了一个表,终于明白了。菜鸟还要努力啊!搞ACM这么长时间连个动态规划还处于入门阶段……
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;
int main(void){
#ifndef ONLINE_JUDGE
freopen("2602.in", "r", stdin);
#endif
const int N = 1000+10;
int t, n, V, w[N], c[N], f[N];
scanf("%d", &t);
while (t--){
scanf("%d%d", &n, &V);
for (int i = 1; i <= n; ++i) scanf("%d", w+i);
for (int i = 1; i <= n; ++i) scanf("%d", c+i);
memset(f, 0, sizeof(f));
for (int i = 1; i <= n; ++i){
for (int v = V; v >= c[i]; --v){
f[v] = max(f[v], f[v-c[i]] + w[i]);
}
}
printf("%d\n", f[V]);
}
return 0;
}
引用一个博客,用于理解01背包,尤其是那个图表,自己画一遍,就基本理解为什么可以优化成一维数组了!