题目链接:点击打开链接
AC代码:
#include <cstdio> #include <iostream> #include <cstring> using namespace std ; int E,F,t,p[10005],w[10005],n ; int dp[100005] ; int INF = 1e9 ; int main() { while(scanf("%d",&t)!=EOF) { while(t--) { scanf("%d%d",&E,&F) ; scanf("%d",&n) ; for(int i = 0 ;i<n ;i++) scanf("%d%d",&p[i],&w[i]) ; for(int i = 1 ;i<10005 ;i++) { dp[i] = INF ; } dp[0] = 0 ; for(int i = 0 ;i<n ;i++) { for(int j = w[i] ;j<=F-E ;j++) { dp[j] = min(dp[j],dp[j-w[i]]+p[i]) ; } } if(dp[F-E] == INF) { printf("This is impossible.\n") ; } else { printf("The minimum amount of money in the piggy-bank is %d.\n",dp[F-E]) ; } } } }
原文地址:http://blog.csdn.net/u012566693/article/details/45174581