1. 程式人生 > >1114 Piggy-Bank

1114 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;
}