zoukankan      html  css  js  c++  java
  • 悼念512汶川大地震遇难同胞——珍惜现在,感恩生活 hdu2191(01背包)

    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
    ", dp[n]);
    
        }
        return 0;
    }
    View Code
  • 相关阅读:
    C语言面试题——寻找错误
    C语言的声明解释的在线工具——cdecl
    C语言面试题——指针运算
    const 指针与指向const的指针
    C语言复杂声明解释
    poj1248
    poj1750
    poj1484
    poj1853
    poj1575
  • 原文地址:https://www.cnblogs.com/daydayupacm/p/5742778.html
Copyright © 2011-2022 走看看