POJ 1384

时间:2022-12-23 12:06:08

求猜存钱罐中至少有多少钱。容易知道金币总的重量,接着背包。

#include<cstdio>
#include<iostream>
using namespace std;
#define N 10010
#define M 100000000 int dp[N];
int val[],we[]; int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int em,fu;
scanf("%d%d",&em,&fu);
int n;
scanf("%d",&n);
for(int i=; i<n; i++)
scanf("%d%d",&val[i],&we[i]);
int ff=fu-em;
for(int i=; i<=ff; i++)
dp[i]=M;
dp[]=; for(int i=; i<n; i++)
{
for(int j=we[i]; j<=ff; j++)
dp[j]=min(dp[j],dp[j-we[i]]+val[i]);
}
if(dp[ff] ==M)printf("This is impossible.\n");
else printf("The minimum amount of money in the piggy-bank is %d.\n", dp[ff]); }
return ;
}