hdu 1114 Piggy-Bank
http://acm.hdu.edu.cn/showproblem.php?pid=1114
完全背包。
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #define maxn 50000 5 using namespace std; 6 const int inf=1<<28; 7 8 int dp[maxn]; 9 int p[maxn],w[maxn]; 10 int t,n,e,f; 11 12 int main() 13 { 14 scanf("%d",&t); 15 while(t--) 16 { 17 scanf("%d%d",&e,&f); 18 scanf("%d",&n); 19 for(int i=1; i<=n; i++) 20 { 21 scanf("%d%d",&p[i],&w[i]); 22 } 23 for(int j=1; j<=f-e; j++) 24 { 25 dp[j]=inf; 26 } 27 dp[0]=0; 28 for(int i=1; i<=n; i++) 29 { 30 for(int j=w[i]; j<=f-e; j++) 31 { 32 dp[j]=min(dp[j],dp[j-w[i]]+p[i]); 33 } 34 } 35 if(dp[f-e]==inf) 36 printf("This is impossible.\n"); 37 else 38 printf("The minimum amount of money in the piggy-bank is %d.\n",dp[f-e]); 39 } 40 return 0; 41 }