http://poj.org/problem?id=1014
最简单之多重背包
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 num[7], DP[60005], F; 26 27 void ZeroOnePack(int v) 28 { 29 for(int i=F;i>=v;i--) 30 { 31 DP[i] = max(DP[i], DP[i-v]+v); 32 } 33 } 34 35 void CompletePack(int v) 36 { 37 for(int i=v;i<=F;i++) 38 { 39 DP[i] = max(DP[i], DP[i-v]+v); 40 } 41 } 42 43 int ComplexPack() 44 { 45 for(int i=1;i<=6;i++) 46 { 47 if(num[i]*i >= F) CompletePack(i); 48 else 49 { 50 int k = 1; 51 while(k <= num[i]) 52 { 53 ZeroOnePack(k*i); 54 num[i] -= k; 55 k *= 2; 56 } 57 ZeroOnePack(num[i]*i); 58 } 59 } 60 return DP[F]; 61 } 62 63 int main() 64 { 65 int T=0; 66 while(scanf("%d%d%d%d%d%d", &num[1],&num[2],&num[3],&num[4],&num[5],&num[6])) 67 { 68 if(!(num[1]||num[2]||num[3]||num[4]||num[5]||num[6])) break; 69 F = 0; mem0(DP); 70 for(int i=1;i<=6;i++) F += num[i]*i; 71 printf("Collection #%d: ", ++T); 72 if(F & 1) {printf("Can't be divided. "); continue;} 73 F /= 2; 74 printf("%s ", ComplexPack()==F?"Can be divided.":"Can't be divided."); 75 } 76 return 0; 77 }