完全背包——HDU-1114

题目含义

给出存钱罐空时和满时的重量,以及一些钱币的价值和重量

每种钱可以取无数次,问装满存钱罐的最小价值

题目分析

很明显,一个完全背包,并且要求刚好装满,又是求最小值,就可以将dp[i]除dp[0]以外赋为INF

题目代码

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
typedef long long LL;
const int maxn=1e4+7;;
const int INF=0x3f3f3f3f;
int t,e,f,w,n;
struct node{
    int wei;
    int val;
}coin[507];
int dp[maxn];
int main(){
    scanf("%d",&t);
    while(t--){
        scanf("%d%d%d",&e,&f,&n);
        w=f-e;
        for(int i=1;i<=n;i++)
            scanf("%d%d",&coin[i].val,&coin[i].wei);
        memset(dp,INF,sizeof(dp));
        dp[0]=0;
        for(int i=1;i<=n;i++)
        for(int j=coin[i].wei;j<=w;j++){
            dp[j]=min(dp[j],dp[j-coin[i].wei]+coin[i].val);
        }
        if(dp[w]<INF)printf("The minimum amount of money in the piggy-bank is %d.\n",dp[w]);
        else printf("This is impossible.\n");
    }
    return 0;
}

 

posted @ 2019-07-23 15:34  helman78  阅读(131)  评论(0编辑  收藏  举报