首页 > 其他 > 详细

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

时间:2016-08-05 21:34:15      阅读:155      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!