题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1114
简单完全背包,不多说。
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <map>
#include <iterator>
#include <vector>
using namespace std;
typedef long long LL; int T;
int E,F;
int dp[];
int p[],w[];
const int INF = ; int main(){
scanf("%d",&T);
while( T-- ){
scanf("%d%d",&E,&F);
F -= E;
int n;
scanf("%d",&n);
for(int i=;i<;i++) dp[i] = INF;
dp[] = ;
for(int i=;i<=n;i++) scanf("%d%d",&p[i],&w[i]);
for(int i=;i<=n;i++){
for(int j=w[i];j<=F;j++){
dp[j] = min(dp[j],dp[j-w[i]]+p[i]);
}
}
if( dp[F]!=INF )
printf("The minimum amount of money in the piggy-bank is %d.\n",dp[F]);
else
puts("This is impossible.");
}
return ;
}