Bone Collector hdu2602

来源:互联网 发布:java策略模式 工厂模式 编辑:程序博客网 时间:2024/06/14 08:39

Bone Collector hdu2602

标签:01背包


题目链接

/*    01背包水题, 模板(节省一维空间)(N个物品, V容量背包, volume: 物品体积, value:物品价值)        for(int i = 0; i < N; i++)            for(int j = V; j >= volume[i]; j--)                dp[j] = max(dp[j], dp[j - volume[i]] + value[i]);        printf("%d\n", dp[V]);    很棒的讲解:http://blog.csdn.net/stack_queue/article/details/53544109    练习:http://blog.csdn.net/yexiaohhjk/article/details/50229489*/#include <stdio.h>#include <string.h>#include <algorithm>using namespace std;const int maxn = 1005;int value[maxn], volume[maxn], dp[maxn];int main(){    int T;    scanf("%d", &T);    while(T--)    {        int N, V;        scanf("%d %d", &N, &V);        for(int i = 0; i < N; i++)  scanf("%d", &value[i]);        for(int i = 0; i < N; i++)  scanf("%d", &volume[i]);        memset(dp, 0, sizeof(dp));  //ZeroOnePack template        for(int i = 0; i < N; i++)            for(int j = V; j >= volume[i]; j--)                dp[j] = max(dp[j], dp[j - volume[i]] + value[i]);        printf("%d\n", dp[V]);    }    return 0;}
原创粉丝点击