1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | /** Notes : 1. 0/1 Knapsack problem 2. u cant take an object more than one time. suppose, u can stand with maximum 30 kg weight an object has a weight of 10 kg but u can take the object only once, not 3 times, though u still have 20 kg space 3. calculate all the values up to 30 and save in an array dp[30] if ur maxWeightLimit = i, thn dp[i] = maxValue u can get */ #include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <string> #include <cctype> #include <stack> #include <queue> #include <list> #include <vector> #include <map> #include <sstream> #include <cmath> #include <bitset> #include <utility> #include <set> #define INT_MAX 2147483647 #define INT_MIN -2147483648 #define N 1000000 using namespace std; int main() { int testCase; scanf ( "%d" , &testCase); while (testCase--) { int objects; int values[1000 + 5]; int weight[1000 + 5]; scanf ( "%d" , &objects); for ( int i = 0; i < objects; ++i) scanf ( "%d %d" , &values[i], &weight[i]); int dp[30 + 5]; int last_added[30 + 5]; memset (dp, 0, sizeof (dp)); for ( int j = 0; j < objects; ++j) { for ( int i = 32; i >= 0; --i) { if (weight[j] <= i && dp[i] < dp[i - weight[j]] + values[j]) { dp[i] = dp[i - weight[j]] + values[j]; last_added[i] = j; } } } int g, volume, aux; scanf ( "%d" , &g); while (g--) { scanf ( "%d" , &volume); printf ( "Max value = $%d\n" , dp[volume]); // print solution aux = volume; while ((aux > 0) && (last_added[aux] != -1)) { printf ( "Added object %d ($%d %dKg). Space left: %d\n" , last_added[aux] + 1, values[last_added[aux]], weight[last_added[aux]], aux - weight[last_added[aux]]); aux -= weight[last_added[aux]]; } } } return 0; } </set></utility></bitset></cmath></sstream></map></vector></list></queue></stack></cctype></string></cstring></algorithm></cstdio></iostream> |
Nome | Comentário | |
---|---|---|
Ainda não há nenhum problema relacionado a esse conteúdo |