题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1114
简单完全背包,不多说。
1 #include <cstdio> 2 #include <algorithm> 3 #include <cstring> 4 #include <cmath> 5 #include <map> 6 #include <iterator> 7 #include <vector> 8 using namespace std; 9 typedef long long LL; 10 11 int T; 12 int E,F; 13 int dp[11111]; 14 int p[555],w[555]; 15 const int INF = 99999999; 16 17 int main(){ 18 scanf("%d",&T); 19 while( T-- ){ 20 scanf("%d%d",&E,&F); 21 F -= E; 22 int n; 23 scanf("%d",&n); 24 for(int i=1;i<11111;i++) dp[i] = INF; 25 dp[0] = 0; 26 for(int i=1;i<=n;i++) scanf("%d%d",&p[i],&w[i]); 27 for(int i=1;i<=n;i++){ 28 for(int j=w[i];j<=F;j++){ 29 dp[j] = min(dp[j],dp[j-w[i]]+p[i]); 30 } 31 } 32 if( dp[F]!=INF ) 33 printf("The minimum amount of money in the piggy-bank is %d.\n",dp[F]); 34 else 35 puts("This is impossible."); 36 } 37 return 0; 38 }
时间: 2024-10-16 15:07:14