码迷,mamicode.com
首页 > 其他好文 > 详细

悼念512汶川大地震遇难同胞——珍惜现在,感恩生活 hdu2191(01背包)

时间:2016-08-05 21:36:20      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:

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

 

悼念512汶川大地震遇难同胞——珍惜现在,感恩生活 hdu2191(01背包)

标签:

原文地址:http://www.cnblogs.com/daydayupacm/p/5742778.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!