完全背包基础

本文介绍了一道经典背包问题——HDU1114,并使用完全背包算法进行解答。通过实例演示了如何初始化dp数组,以及如何更新dp数组来找到装满背包所需的最小价值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目:hdu1114

题意:一个背包,求装满状态下能乘的最小价值

解答:完全背包~~~

//完全背包
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const int maxn = 550;
const int MAXN = 10010;
const int INF = 0x3f3f3f3f;
struct Goods
{
    int w,v;
};
Goods goods[maxn];
int dp[MAXN];
int main()
{
    int T,E,F,N,W;
    scanf("%d",&T);
    while(T--)
    {
        memset(dp,0,sizeof(dp));
        scanf("%d%d",&E,&F);
        W = F - E;
        for(int i = 0;i <= W;i++)
            dp[i] = INF;
        dp[0] = 0;///!!!
        scanf("%d",&N);
        for(int i = 1;i <= N;i++)
            scanf("%d%d",&goods[i].v,&goods[i].w);
        for(int i = 1;i <= N;i++)
            for(int j = 0;j <= W;j++)
                if(j >= goods[i].w)
            dp[j] = min(dp[j],dp[j-goods[i].w]+goods[i].v);
        if(dp[W] == INF)
            printf("This is impossible.\n");
        else
            printf("The minimum amount of money in the piggy-bank is %d.\n",dp[W]);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值