描述

问题链接
hdu 2602 Bone Collector

题解

纯粹的/1背包问题
一维dp[]

 for(int i=0;i<n;i++){
            for(int j=v;j>=weight[i];j--){
                dp[j]=max(dp[j],dp[j-weight[i]]+value[i]);
            }
        }

代码

#include <iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define maxn 1005
int n,v,weight[maxn],value[maxn],dp[maxn];
int main()
{
    int T;
    scanf("%d",&T);
    while(T--){
        memset(dp,0,sizeof(dp));
        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",&weight[i]);
        }
        for(int i=0;i<n;i++){
            for(int j=v;j>=weight[i];j--){
                dp[j]=max(dp[j],dp[j-weight[i]]+value[i]);
            }
        }
        printf("%d\n",dp[v]);
    }
    return 0;
}

相关文章:

  • 2022-12-23
  • 2021-12-05
  • 2021-08-31
  • 2021-06-28
  • 2021-06-04
  • 2021-07-19
猜你喜欢
  • 2022-01-16
  • 2022-03-07
  • 2021-09-23
  • 2022-12-23
相关资源
相似解决方案