hdu1114 Piggy-Bank(完全背包)


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

题意:存钱罐可以往里面放一些价值小的钱,但是时间久了就不知道里面有多少钱了,除非你打破它。现在给出空罐子的重量和最满能装到多重,然后给出每种硬币的价值和重量,我们要在不打破它的情况下确认罐子里最少有多少钱。


思路:很贴近生活。每种硬币数量不限,所以是完全背包。条件必须在装满的情况下,求最小价值,那么初始化变为装满的情况(背包九讲)和max变为min。如果遍历完物品后满容量的dp值没有被改变则说明这些硬币无法组成这个容量。由于数据10000,所以就不用二维的了。


#include <stdio.h>
#include <algorithm>
#include <stdlib.h>
#include <string.h>
#include <iostream>

using namespace std;

typedef long long LL;

const int N = 10005;
const int INF = 0x3f3f3f3f;

int dp[N];

int main()
{
  //  freopen("in.txt", "r", stdin);
    int t, V, n, emp, fil;
    int cost[N], weight[N];
    scanf("%d", &t);
    while(t--)
    {
        scanf("%d%d", &emp, &fil);
        scanf("%d", &n);//物品数量
        for(int i = 1; i <= n; i++)
            scanf("%d%d", &weight[i], &cost[i]);
        V = fil-emp;
        dp[0] = 0;//装满
        for(int i = 1; i <= V; i++)
            dp[i] = INF;
        for(int i = 1; i <= n; i++)
            for(int j = cost[i]; j <= V; j++)
            {
                dp[j] = min(dp[j], dp[j-cost[i]]+weight[i]);
            }
    /*    for(int i = 0; i <= V; i++)
            printf("%d ", dp[i]);
        printf("\n");*/
        if(dp[V] == INF) printf("This is impossible.\n");
        else printf("The minimum amount of money in the piggy-bank is %d.\n", dp[V]);
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值