【题解】hdu2191 01背包

题目链接

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=4e4+10;
int a[N],dp[N],w[N];
int main()
{
    //freopen("in.txt","r",stdin);
    int C;
    scanf("%d",&C);
    while(C--)
    {
        int n,m,p,h,c,i,j,k=0;
        //n经费p价格h重量 
        scanf("%d%d",&n,&m);
        memset(dp,0,sizeof(dp));
        for(i=1;i<=m;i++)
        {
            scanf("%d%d%d",&p,&h,&c);
            while(c--)
            {
                a[++k]=h;//a存重量(价值)
                w[k]=p;//w存费用 
            }
        }
        for(i=1;i<=k;i++)
        for(j=n;j>=w[i];j--)
        dp[j]=max(dp[j],dp[j-w[i]]+a[i]);
        printf("%d\n",dp[n]);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41958841/article/details/81636209