hdu 1114 Piggy-Bank 完全背包

本文详细介绍了一个经典的完全背包问题实例,并提供了完整的C++代码实现。通过分析如何求解最小花费问题,文章展示了初始化数组、填充过程及判断无解情况的具体步骤。

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1114

 

近乎裸的完全背包求最小

dp[0] = 0;

后面的用INF填充 【用fill的话记得前后都要 “+1”啊血泪...】

 

如果dp[V]还是INF说明无解

 

#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <stack>
#include <set>
#include <queue>
#include <vector>

using namespace std;

typedef long long ll;

const int maxn = 510;
const int maxm = 10100;
const int INF = 2000000000;

int w[maxn], c[maxn];
int f[maxm];
int n, V;

void CompletePack(int cost, int weight)
{
    for(int i = cost; i <= V; i++)
    {
        f[i] = min(f[i], f[(i-cost)] + weight);
    }
}

int main()
{
    //freopen("in.txt", "r", stdin);

    int T;
    scanf("%d", &T);
    while(T--)
    {
        int E, F;
        scanf("%d%d", &E, &F);
        V = F - E;

        f[0] = 0;
        fill(f + 1, f + V + 1, INF);

        int n;
        scanf("%d", &n);
        for(int i = 0; i < n; i++)
            scanf("%d%d", &w[i], &c[i]);

        for(int i = 0; i < n; i++)
        {
            CompletePack(c[i], w[i]);
        }

        if(f[V] == INF)
            printf("This is impossible.\n");
        else
            printf("The minimum amount of money in the piggy-bank is %d.\n", f[V]);
    }

    return 0;
}

 

转载于:https://www.cnblogs.com/dishu/p/4296346.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值