九度OJ 1455 珍惜现在,感恩生活 -- 动态规划(背包问题)

题目地址:http://ac.jobdu.com/problem.php?pid=1455

 

题目描述:

为了挽救灾区同胞的生命,心系灾区同胞的你准备自己采购一些粮食支援灾区,现在假设你一共有资金n元,而市场有m种大米,每种大米都是袋装产品,其价格不等,并且只能整袋购买。请问:你用有限的资金最多能采购多少公斤粮食呢?

输入:

输入数据首先包含一个正整数C,表示有C组测试用例,每组测试用例的第一行是两个整数n和m(1<=n<=100, 1<=m<=100),分别表示经费的金额和大米的种类,然后是m行数据,每行包含3个数p,h和c(1<=p<=20,1&lt;=h<=200,1<=c<=20),分别表示每袋的价格、每袋的重量以及对应种类大米的袋数。

输出:

对于每组测试数据,请输出能够购买大米的最多重量,你可以假设经费买不光所有的大米,并且经费你可以不用完。每个实例的输出占一行。

样例输入:
1
8 2
2 100 4
4 100 2
样例输出:
400
#include <stdio.h>
 
typedef struct rice{
    int price;
    int weight;
}Rice;
 
int max (int a, int b){
    return (a > b) ? a : b;
}
 
int main (void){
    int C, m, n;
    Rice ric[2001];
    int dp[101];
    int i, j;
    int price, weight, num, cnt, c;
 
    scanf ("%d", &C);
    while (C-- != 0){
        scanf ("%d%d", &n, &m);
        cnt = 0;
        for (i=1; i<=m; ++i){
            scanf ("%d%d%d", &price, &weight, &num);
            c = 1;
            while (num -c > 0){
                num -= c;
                ric[++cnt].price = price * c;
                ric[cnt].weight = weight * c;
                c *= 2;
            }
            ric[++cnt].price = price * num;
            ric[cnt].weight = weight * num;
        }
        for (i=0; i<=n; ++i)
            dp[i] = 0;
        for (i=1; i<=cnt; ++i){
            for (j=n; j>=ric[i].price; --j){
                dp[j] = max (dp[j], dp[j-ric[i].price] + ric[i].weight);
            }
        }
        printf ("%d\n", dp[n]);
    }
 
    return 0;
}

 

参考资料:多重背包问题

 

转载于:https://www.cnblogs.com/liushaobo/p/4373828.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值