HDU 2191(多重背包)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2191
多重背包的模板题

代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cmath>
#include <cstring>
#include <string>
#include <vector>
#include <set>
#include <stack>
#include <list>
#include <map>
#define P(x) x>0?x:0
#define INF 0x3f3f3f3f

using namespace std;
typedef long long ll;
typedef vector<int>:: iterator VITer;
const int maxN=105;

int n,m;
int dp[maxN],ans;
struct node
{
    int p,w,num;
    node(int a=0,int b=0,int c=0):p(a),w(b),num(c){}
    friend bool operator < (node n1,node n2)
    {
        return n1.w<n2.w;
    }
}rice[maxN];

void init()
{
    memset(dp,0, sizeof(dp));
    ans=0;
}

int main()
{
    int C;
    scanf("%d",&C);
    while(C--)
    {
        init();
        scanf("%d%d",&n,&m);
        for(int i=1;i<=m;i++)
        {
            scanf("%d%d%d",&rice[i].p,&rice[i].w,&rice[i].num);
        }
        for(int i=1;i<=m;i++)
        {
            for(int k=1;k<=rice[i].num;k++)
            {
                for(int j=n;j>=k*rice[i].p;j--)
                {
                    dp[j]=max(dp[j],dp[j-rice[i].p]+rice[i].w);
                }
            }
        }
        for(int i=1;i<=n;i++)
        {
            ans=max(ans,dp[i]);
        }
        printf("%d\n",ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_44049850/article/details/94653303