HDU - 1114 Piggy-Bank

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_37129433/article/details/82820590

Piggy-Bank

题 意:给你一个存钱罐,空存钱罐的重量为E,现在存钱罐的总重量为F,由n种面值的硬币,每种硬币的面值为val[i],重量为cost[i].现在问你存钱罐里最少有多少钱。
数据范围:
1<=E<=F<=1e4
1<=N<=500
1<=val[i]<=cost[i]<=5e4
输入样例:

3
10 110
2
1 1
30 50
10 110
2
1 1
50 30
1 6
2
10 3
20 4

输出样例

The minimum amount of money in the piggy-bank is 60.
The minimum amount of money in the piggy-bank is 100.
This is impossible.

思 路:这个是完全背包的模板题,但是这个初始化,我还是很满意的。其实也没啥好讲的,(lll¬ω¬),骗访问量。

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<string>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define debug(x) cout<<#x<<" = "<<(x)<<endl;
using namespace std;
typedef long long ll;
typedef pair<int,int>P;
const ll mod = 998244353;
const int INF = 0x3f3f3f3f;
const int maxn = 1e4 + 5;
int dp[maxn];
int val[maxn],cost[maxn];
int n,w;
int E,F;
int t;
int main() {
    //freopen("input.txt","r",stdin);
    scanf("%d",&t);
    while(t--){
        scanf("%d %d",&E,&F);
        w = F-E;
        scanf("%d",&n);
        memset(dp,INF,sizeof(dp));
        dp[0] = 0;
        for(int i=1;i<=n;i++){
            scanf("%d %d",&val[i],&cost[i]);
        }
        for(int i=1;i<=n;i++){
            for(int j=cost[i];j<=w;j++){
                dp[j] = min(dp[j],dp[j-cost[i]]+val[i]);
            }
        }
        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;
}

猜你喜欢

转载自blog.csdn.net/qq_37129433/article/details/82820590