题意: 给出空储钱罐的重量和装满时的重量,然后给出储钱罐里面钱币的种类和重量,求在满罐时里面钱最少为多少?
分析: 因为每种钱币的数量不只一个,是个完全背包,每次找钱数最少的,
# define inf 0x3f3f3f3f
const int maxn=550;
int price[maxn];
int weight[maxn];
int dp[10010];
int main()
{
int t;
cin>>t;
while(t--)
{
int a,b;
while(scanf("%d%d",&a,&b)!=EOF)
{
int C=b-a;
for(int i=1;i<=C;i++)
dp[i]=inf;
int n;
cin>>n;
for(int i=0;i<n;i++)
scanf("%d%d",&price[i],&weight[i]);
dp[0]=0;
for(int i=0;i<n;i++)
{
for(int j=weight[i]; j<=C; j++)
{
dp[j]=min(dp[j],dp[j-weight[i]]+price[i]);
}
}
if(dp[C]>=inf)
printf("This is impossible.\n");
else
printf("The minimum amount of money in the piggy-bank is %d.\n",dp[C]);
}
}
return 0;
}
分析: 完全背包,物品只有三种,每种有价格,每次找最大。