POJ1384Piggy-Bank(DP)
POJ1384http://poj.org/problem?id=1384
最简单的完全背包问题,注意下初始化为INF就可以。
1 #include <map> 2 #include <set> 3 #include <stack> 4 #include <queue> 5 #include <cmath> 6 #include <ctime> 7 #include <vector> 8 #include <cstdio> 9 #include <cctype> 10 #include <cstring> 11 #include <cstdlib> 12 #include <iostream> 13 #include <algorithm> 14 using namespace std; 15 #define INF 0x3f3f3f3f 16 #define MAX(a,b) (a > b ? a : b) 17 #define MIN(a,b) (a < b ? a : b) 18 #define mem0(a) memset(a,0,sizeof(a)) 19 20 typedef long long LL; 21 const double eps = 1e-12; 22 const int MAXN = 1005; 23 const int MAXM = 5005; 24 25 int T, N, E, F; 26 int DP[11000], P[550], W[550]; 27 28 int main() 29 { 30 scanf("%d", &T); 31 while(T--) 32 { 33 scanf("%d %d", &E, &F); 34 F -= E; 35 for(int i=0;i<=F;i++) DP[i] = INF; 36 scanf("%d", &N); 37 for(int i=0;i<N;i++) scanf("%d %d", &P[i], &W[i]); 38 DP[0] = 0; 39 for(int i=0;i<N;i++) 40 { 41 for(int j=W[i];j<=F;j++) 42 { 43 if(DP[j] > DP[j-W[i]] + P[i]) 44 { 45 DP[j] = DP[j-W[i]] + P[i]; 46 } 47 } 48 } 49 if(DP[F] == INF) printf("This is impossible.\n"); 50 else printf("The minimum amount of money in the piggy-bank is %d.\n", DP[F]); 51 } 52 return 0; 53 }