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的

i \ v 1 2 3 4 5 6 7 8 9 10 1 0 0 0 0 1 1 1 1 1 1 2 0 0 0 2 2 2 2 2 3 3 3 0 0 3 3 3 3 5 5 5 5 4 0 0 4 4 7 7 7 7 9 9 5 0 5 5 9 9 12 12 12 12 14

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;}
0 0
原创粉丝点击