【题目链接】
http://acm.hdu.edu.cn/showproblem.php?pid=1114
题目意思
给一个存钱罐初始重量和最终重量,给你n种银币,每种银币有对应的价值和重量,问存钱罐里面最低的价值是多少,如果没法得到输出“This is impossible.”
解题思路
由于要正好得到,所以初值要赋成INF,然后跑一边完全背包就可以了
代码部分
#include <bits/stdc++.h>
using namespace std;
#define LL long long
const int maxn=10005;
const int INF=0x3f3f3f;
int dp[maxn];
int a[505],b[505];
int main()
{
int n,m,t;
scanf("%d",&t);
while(t--)
{
scanf("%d %d",&n,&m);
m=m-n;
scanf("%d",&n);
for (int i=1;i<=m;i++)
dp[i]=INF;
dp[0]=0;
for (int i=1; i<=n; i++)
scanf("%d %d",&b[i],&a[i]);
for (int i=1; i<=n; i++)
{
for (int j=a[i]; j<=m; j++)
dp[j]=min(dp[j],dp[j-a[i]]+b[i]);
}
if (dp[m]!=INF)
printf("The minimum amount of money in the piggy-bank is %d.\n",dp[m]);
else printf("This is impossible.\n");
}
return 0;
}