HDOJ 2602 Bone Collector

题目地址:

http://acm.hdu.edu.cn/showproblem.php?pid=2602

貌似是HDOJ中少数的几个01背包之一~~~很经典的题目。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// www.wutianqi.com
// Author: Tanky Woo
// HDOJ 2602
#include <iostream>
using namespace std;
 
int main()
{
int nCases;
int nPack, nMaxVolume;
int weight[1002], value[1002];
int record[1002];
    //freopen("input.txt", "r", stdin);
    scanf("%d", &nCases);
    while(nCases--)
    {
        memset(record, 0, sizeof(record));
        scanf("%d %d", &nPack, &nMaxVolume);
        for(int i=0; i<nPack; ++i)
            scanf("%d", &value[i]);
        for(int i=0; i<nPack; ++i)
            scanf("%d", &weight[i]);
        for(int i=0; i<nPack; ++i)
            for(int j=nMaxVolume; j>=weight[i]; --j)
            {
                if(record[j-weight[i]]+value[i] > record[j])
                    record[j] = record[j-weight[i]]+value[i];
            }
        printf("%d\n", record[nMaxVolume]);
    }
    return 0;
}

发布者

Tanky Woo

Tanky Woo,[个人主页:https://tankywoo.com] / [新博客:https://blog.tankywoo.com]