HDU 1114 Piggy-Bank 动态规划完全背包
http://acm.hdu.edu.cn/showproblem.php?pid=1114
题意:
有T组测试数据,后面的E和F分别表示存钱罐空的时候的重量和满了的时候的重量 , 然后有个m ,
代表下面有 m 种钱币 , 每种钱币分别有他的面值和重量 , 要你求出当存钱罐满的时候 , 存钱罐中至少
有多少钱.
坑爹:
因为他是要求出最少有多少钱 , 所以要 DP[ j ] = min ( DP [ j ] , DP[ j - cost ] + weight )
还有一点要注意的是DP数组的初始化 , 要初始化为最大值 , 因为它每次都要求最小的一种情况 , DP[0] = 0
是递推的一个开始.
解法:
跟普通的完全背包差不多 , 平时的是要求最大的 , 而这个是要求最小的 , 反过来想就行了.

1 #include<iostream> 2 using namespace std; 3 4 const int maxn = 50000 + 10; 5 const int INF = 0x3fffffff; 6 int DP[maxn]; 7 int n; 8 9 int min(int a,int b) 10 { 11 return a < b ? a : b; 12 } 13 14 void CompletePack(int cost,int weight) 15 { 16 int j; 17 for(j=cost; j<=n; j++) 18 { 19 DP[j] = min(DP[j] , DP[j-cost]+weight); 20 } 21 } 22 23 struct Node 24 { 25 int x; 26 int y; 27 }; 28 29 struct Node A[maxn]; 30 31 int main() 32 { 33 int T; 34 cin>>T; 35 while(T--) 36 { 37 // memset(DP,0,sizeof(DP)); 38 39 int a; 40 int b; 41 cin>>a>>b; 42 n = b - a; 43 44 int m; 45 cin>>m; 46 47 int i; 48 49 for(i=0; i<maxn; i++) 50 { 51 DP[i] = INF; 52 } 53 DP[0] = 0; 54 for(i=1; i<=m; i++) 55 { 56 cin>>A[i].x>>A[i].y; 57 } 58 59 for(i=1; i<=m; i++) 60 { 61 CompletePack(A[i].y , A[i].x); 62 } 63 64 if(DP[n] != INF) 65 { 66 printf("The minimum amount of money in the piggy-bank is %d.\n",DP[n]); 67 } 68 else 69 { 70 printf("This is impossible.\n"); 71 } 72 73 } 74 75 return 0; 76 }