这是个完全背包的问题,套模板吧!
#include<stdio.h>
#define min(a,b) a<b?a:b
#define inf 6000005
int f[10005];
int main()
{
int i,j,n,t,E,F,W;
int p[505],w[505];
while(scanf("%d",&t)!=EOF)
{
while(t--){
scanf("%d%d%d",&E,&F,&n);
for(i=1;i<=n;i++) scanf("%d%d",&p[i],&w[i]);
W=F-E;
for(i=1;i<=W;i++) f[i]=inf;
f[0]=0;
for(i=1;i<=n;i++){
for(j=w[i];j<=W;j++){
f[j]=min(f[j],f[j-w[i]]+p[i]);
}
}
if(f[W]==inf) printf("This is impossible.\n");
else printf("The minimum amount of money in the piggy-bank is %d.\n",f[W]);
}
}
return 0;
}