http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1085
http://hihocoder.com/problemset/problem/1038?sid=320857
The contents of the N items are taken out in a backpack of w, and the volume of each item is W1,W2 ... Wn (WI is an integer), corresponding to the value of P1,P2 ... Pn (pi is an integer). Find the maximum value a backpack can hold.
Dynamic transfer Equation Dp[i][j]=max (Dp[i-1][j],dp[i-1][j-v[i]]+w[i]);
Backpack Nine says it's very good.
1#include <iostream>2#include <cstdio>3#include <cmath>4#include <vector>5#include <cstring>6#include <string>7#include <algorithm>8#include <string>9#include <Set>Ten#include <functional> One#include <numeric> A#include <sstream> -#include <stack> -#include <map> the#include <queue> - - #defineCL (arr, Val) memset (arr, Val, sizeof (arr)) - + #definell Long Long - #defineINF 0x7f7f7f7f + #defineLC L,m,rt<<1 A #defineRC M + 1,r,rt<<1|1 at #definePi ACOs (-1.0) - - #defineL (x) (x) << 1 - #defineR (x) (x) << 1 | 1 - #defineMID (L, R) (L + R) >> 1 - #defineMin (x, y) (x) < (y)? (x): (y) in #defineMax (x, y) (x) < (y)? (y): (x) - #defineE (x) (1 << (x)) to #defineIabs (x) (x) < 0? -(x): (x) + #defineOut (x) printf ("%i64d\n", X) - #defineLowbit (x) (x) & (-X) the #defineRead () freopen ("A.txt", "R", stdin) * #defineWrite () freopen ("B.txt", "w", stdout); $ #defineMAXN 1000000000Panax Notoginseng #defineN 100010 - using namespacestd; the + intn,m; A intF[n]; the voidZeroonepack (intCostintweight) + { - for(intv=m;v>=cost;v--) $F[v]=max (f[v],f[v-cost]+weight); $ } - intMain () - { the //Read (); - //Write ();Wuyi intb; the while(~SCANF ("%d%d",&n,&m)) - { WuMemset (F,0,sizeof(f)); - for(intI=0; i<n;i++) About { $scanf"%d%d",&a,&b); - Zeroonepack (A, b); - } -printf"%d\n", F[m]); A } + return 0; the}
Basic 01 Knapsack problem