描述

多重背包

题解

多重背包
求解时,注意计算的次序问题,因为从后面计算到前面,所以第一维要从后面到前面开始枚举。

代码

#include <iostream>
#include <cstdio>
#include<string.h>
using namespace std;
#define maxn1 105
#define maxn2 50005
int N,W;
struct node{
    int w,p,c;
}a[maxn1];
int dp[maxn1][maxn2];

int main()
{
    while(scanf("%d%d",&N,&W)==2){
        memset(dp,0,sizeof(dp));
        for(int i=0;i<N;i++){
            scanf("%d%d%d",&a[i].w,&a[i].p,&a[i].c);
        }
        for(int i=N-1;i>=0;i--){
            for(int j=0;j<=W;j++){
                for(int k=0;k<=a[i].c&&k<=j/a[i].w;k++){
                    dp[i][j]=max(dp[i][j],dp[i+1][j-k*a[i].w]+k*a[i].p);
                }
            }
        }
        printf("%d\n",dp[0][W]);
    }
    return 0;
}

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-05-27
  • 2021-12-01
  • 2022-02-18
  • 2022-01-10
  • 2021-10-07
猜你喜欢
  • 2021-09-30
  • 2021-04-18
相关资源
相似解决方案