HDU 1114 Piggy-Bank

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


题意:给出存钱罐的塞满前后重量,给出各硬币的价值和重量,问里面少 多少钱


思路:经典的完全背包,不作详细解释了


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#define inf 0x3f3f3f3f
using namespace std;

int val[530],wei[530],dp[10030];

int main()
{
    int n,t;
    scanf("%d",&t);
    while (t--)
    {
        int a,b,qua;
        scanf("%d%d%d",&a,&b,&n);
        qua=b-a;


        for (int i=0;i<n;i++)
        {
            scanf("%d%d",&val[i],&wei[i]);
        }

        memset(dp,inf,sizeof(dp));
        dp[0]=0;
        for (int i=0;i<n;i++)
        {
            for (int j=wei[i];j<=qua;j++)
            {
                if (dp[j-wei[i]]==inf) continue;
                if (dp[j-wei[i]]+val[i]<dp[j])
                {
                    dp[j]=dp[j-wei[i]]+val[i];
                }
                //cout<<i<<":"<<dp[j]<<endl;
            }
        }

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




你可能感兴趣的:(HDU 1114 Piggy-Bank)