题目传送门
题目大意
给你一个存钱罐的初始重量和装满后的重量
给你n个可以任意取的硬币,每个硬币具有价值和重量,求将存钱罐装满后硬币的最小价值
若不能装满则输出 This is impossible.
思路
很明显的完全背包问题,因为求的最小值,所以将数组初始化的时候为无穷大,最后判断是否装满即为装满时大小是否为无穷大即可
AC Code
#pragma GCC optimize("-Ofast","-funroll-all-loops")
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<math.h>
using namespace std;
#define endl '\n'
#define INF 0x3f3f3f3f
// #define int long long
#define debug(a) cout<<#a<<"="<<a<<endl;
// #define TDS_ACM_LOCAL
typedef long long ll;
const double PI=acos(-1.0);
const double e=exp(1.0);
const int M=1e9 +7;
const int N=1e5 +9;
inline int mymax(int x,int y){return x>y?x:y;}
inline int mymin(int x,int y){return x<y?x:y;}
inline int read(){
register int x=0, f=0;
register char ch=getchar();
while(ch<'0'||ch>'9') f|=ch=='-',ch=getchar();
while(ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+(ch^48),ch=getchar();
return f?-x:x;
}
inline void write(register int x){
if(x < 0) {putchar('-');x = -x;}
if(x > 9) write(x/10);
putchar(x % 10 + '0');
}
int k1, k, n;
int wei[N], v[N], dp[N];
void solve(){
k1=read(); k=read();
k-=k1;
n=read();
for(int i=1; i<=n; i++) v[i]=read(), wei[i]=read();
memset(dp, INF, sizeof(dp));
dp[0]=0;
for(int i=1; i<=n; i++)
for(int j=wei[i]; j<=k; j++)
dp[j]=min(dp[j], dp[j-wei[i]]+v[i]);
if(dp[k]!=INF)
cout<<"The minimum amount of money in the piggy-bank is "<<dp[k]<<".\n";
else
cout<<"This is impossible.\n";
return ;
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
#ifdef TDS_ACM_LOCAL
freopen("D:\\VS code\\.vscode\\testall\\in.txt", "r", stdin);
freopen("D:\\VS code\\.vscode\\testall\\out.txt", "w", stdout);
#endif
int T;
T=read();
while(T--) solve();
return 0;
}