This problem in fact and the general 01 backpack no difference, but the problem in accordance with normal thinking to do, because the capacity is too large, open an array of 10^9 can not open, so this time need to change the thinking, the problem just started not understand, and later on the internet in the solve, found that is the exchange of weight and value, But has not understood what the meaning, later, carefully thought about that the most basic 01 backpack is how to come out of the only figured out, can not be said to be interchangeable value and weight, so actually can not deepen understanding, after the problem, found that the backpack understand a bit deeper, write a blog, leave a memorial
This problem is very easy to find in fact, the weight is very large, to reach 10^9, but the value is very small ah, now to push the so-called "interchange" is how to come (in fact, I think it is not as far from the original, not called "interchange" good understanding point), the most primitive of the formula
DP[I][J] means the maximum value of the backpack when I is taken, the weight is J, the state transfer equation is dp[i][j] = max (Dp[i-1][j], dp[i-1][j-weight[i]), and the meaning of this expression must be clear White bar, the front of the meaning is not to take the current this, the latter is to take the current item, and then after the space optimization has become dp[j] = max (Dp[j], dp[j-weight[i] + value[i]), carefully observe the two-dimensional array will be found, both states are I- 1, so you can remove, but pay attention to the loop, the time to reverse order, the word is a complete backpack, forget to say this dp[j] mean what, dp[j] is when the weight of J when the maximum value. To figure this out. At this time can come to see this problem, the topic requirements and Common 01 backpack, the maximum value of the request can be installed, the common method is to find the maximum value directly, now want to change the thinking, find the smallest weight, because the same value, the smaller the weight, then the last can be installed value, so this dp[i][j] means that when taken I, the minimum weight when the value is J, the state transfer equation is dp[i][j] = min (Dp[i-1][j], Dp[i-1][j-value[i]] + weight[i]), and the same as the original push, no longer wordy, space optimization after the state transfer equation is Dp[j] = min (Dp[j], Dp[j-value[i]] + weight[i]), same meaning, dp[j] represents the minimum weight at the time of the value J, to the end, as long as the maximum value of the DP array down, as long as the dp[j] <= backpack weight The direct output J, when J is the largest.
The code is as follows:
1#include <stdio.h>2#include <stdlib.h>3#include <string.h>4 #defineMin (A, b) a<b?a:b5 6 Const intN =10003;7 intDp[n];8 intweight[102];9 intvalue[102];Ten intMain () One { A - intN, W; - while(SCANF ("%d%d", &n, &w) = =2) the { - intsum =0; - for(inti =0; I < n; i++) - { +scanf"%d%d", &weight[i], &value[i]); -Sum + = Value[i];//sum saves all the sum of the values + } AMemset (DP,0x3f,sizeof(DP));//initialize the array to infinity, because the minimum value is to be found, so the default infinity atdp[0] =0; - for(inti =0; I < n; i++) - { - - for(intj = sum; J >= Value[i]; j--) -DP[J] = Min (Dp[j], Dp[j-value[i]] +weight[i]); in } - for(inti = sum; I >=0; i--) to if(Dp[i] <=W) + { -printf"%d\n", i); the Break; * } $ }Panax Notoginseng return 0; -}
Nyoj 860 See also 01 backpack