http://acm.hdu.edu.cn/showproblem.php?pid=1114 #include<stdio.h> #include<iostream> using namespace std; const int N=15000; #define inf 999999 int val[N],c[N],dp[N]; int main() { int t,e,f,i,j,n; scanf("%d",&t); while(t--) { scanf("%d%d",&e,&f); int v=f-e; scanf("%d",&n); for(i=1;i<=n;i++) { scanf("%d%d",&val[i],&c[i]); } for(i=1;i<=v;i++)dp[i]=inf;//初始化 注意 dp[0]=0;////初始化 注意 for(i=1;i<=n;i++) { for(j=c[i];j<=v;j++) dp[j]=min(dp[j],dp[j-c[i]]+val[i]); } if(dp[v]!=inf) printf("The minimum amount of money in the piggy-bank is %d.\n",dp[v]); else printf("This is impossible.\n"); } }