1 // accepted 868 kb 188 MS 2 // multiple backpack 3 # include <cstdio> 4 # include <cstring> 5 # include <iostream> 6 # include <queue> 7 # include <cmath> 8 # include <algorithm> 9 Using namespace STD; 10/** 11 * This is a documentation comment block12 * if you can't stick to it one day, think about why you're here! 13 * @ authr songt14 */15 const int imax_v = 40005; 16 const int imax_n = 405; 17 int DP [imax_v]; 18 int N; 19 struct node20 {21 int weight, c, A; 22} f [imax_n]; 23 // sort by A. We use reverse push in DP, therefore, we need to calculate the 24 int CMP (node X, node y) 25 {26 return X when a is small. A <Y. a; 27} 28 int max (int A, int B) 29 {30 return A> B? A: B; 31} 32 void zeroonepack (INT weight, int value, int v) 33 {34 for (Int J = V; j> = weight; j --) 35 {36 DP [J] = max (DP [J], DP [J-weight] + value); 37} 38} 39 void completepack (INT weight, int value, int v) 40 {41 for (Int J = weight; j <= V; j ++) 42 {43 DP [J] = max (DP [J], DP [J-weight] + value); 44} 45} 46 void multiplepack (INT weight, int value, int amount, int v) 47 {48 int K = 1; 49 If (Amount * weight> = V) 50 {51 completepack (weight, Val UE, V); 52 return; 53} 54 while (k <amount) 55 {56 zeroonepack (K * weight, K * value, V); 57 amount-= K; 58 k <= 1; 59} 60 zeroonepack (Amount * weight, amount * value, V); 61} 62 void dp () 63 {64 memset (DP, 0, sizeof (DP); 65 for (INT I = 1; I <= N; I ++) 66 {67 multiplepack (F [I]. weight, F [I]. weight, F [I]. c, F [I]. a); 68} 69 int ans = 0; 70 for (INT I = 1; I <= 40000; I ++) 71 ans = max (ANS, DP [I]); 72 printf ("% d \ n", ANS); 73} 74 int main () 75 {76 while (Scanf ("% d", & N )! = EOF) 77 {78 for (INT I = 1; I <= N; I ++) 79 scanf ("% d", & F [I]. weight, & F [I]. a, & F [I]. c); 80 sort (F + 1, F + n + 1, CMP); 81 dp (); 82} 83 return 0; 84}View code
Poj2392 multiple backpacks