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

 

中文题目,就不说废话了。。。

分析:这道题是多重背包,但是可以用01背包来做,把每种大米有几袋一个一个单独存储,这样就变成01背包了。。。

 

 

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
#include <stack>
#include <math.h>

using namespace std;

#define met(a, b) memset(a, b, sizeof(a))
#define maxn 2500
#define INF 0x3f3f3f3f
const int MOD = 1e9+7;

typedef long long LL;
int dp[maxn];

struct node
{
    int x, y;
}s[maxn];

int main()
{
    int T, n, m, p, h, c;

    scanf("%d", &T);

    while(T --)
    {
        scanf("%d %d", &n, &m);

        int k = 1;

        for(int i=1; i<=m; i++)
        {
            scanf("%d %d %d", &p, &h, &c);
            for(int j=1; j<=c; j++)
              {
                  s[k].x=p;
                  s[k++].y=h;
              }
        }

        memset(dp, 0, sizeof(dp));

        for(int i=1; i<k; i++)
        {
            for(int j=n; j>=s[i].x; j--)
            {
                dp[j] = max(dp[j], dp[j-s[i].x]+s[i].y);
            }
        }

        printf("%d\n", dp[n]);

    }
    return 0;
}
View Code

 

posted on 2016-08-05 21:00  不忧尘世不忧心  阅读(175)  评论(0编辑  收藏  举报