HDU 2602 Bone Collector(DP:01背包)

HDU 2602 Bone Collector(DP:01背包)

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

题意:N个物品,每个物品有体积vi和价值valuei,问总体积不超过V能获得的最大价值.

分析:基础01背包问题.

AC代码:

#include <iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
const int MAXN=1010;
int weight[MAXN],value[MAXN];
int nKind,nWeight;
int dp[MAXN];
void ZeroOnePack()
{
    for(int i=1;i<=nKind;i++)
        for(int j=nWeight;j>=weight[i];j--)
        dp[j]=max(dp[j],dp[j-weight[i]]+value[i]);
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&nKind,&nWeight);
        for(int i=1;i<=nKind;i++)
            scanf("%d",&value[i]);
        for(int i=1;i<=nKind;i++)
            scanf("%d",&weight[i]);
        memset(dp,0,sizeof(dp));
        ZeroOnePack();
        printf("%d\n",dp[nWeight]);
    }
    return 0;
}


你可能感兴趣的:(ACM)