01 knapsack problem

Source: Internet
Author: User

Reference to the Http://www.cnblogs.com/qinyg/archive/2012/04/26/2471829.html

The key state transfer equations are as follows

V (i,j) indicates that the maximum value of an item in a backpack with a capacity of J (1<=j<=c) can be loaded in the first I (1<=I<=N) item, and the following dynamic planning functions are available:

(1) V (i,0) =v (0,j) =0

(2) V (i,j) =v (i-1,j) J<wi

V (i,j) =max{v (i-1,j), V (I-1,J-WI) +vi)} J>wi

(1) type indicates: If the weight of the item I is greater than the capacity of the backpack, then the maximum value that can be obtained by the front I item is the same as the maximum price to be loaded before the i-1 item, i.e. the item I cannot be loaded into the backpack; the first (2) formula indicates that if the weight of item I is less than the capacity of the backpack, there are (a) If the item I is loaded into a backpack, the value of the backpack item is equal to the value of the J-WI in the backpack of the i-1 item, plus the value of the article I, VI; (b) If item I is not loaded into a backpack, the value of the item in the backpack is equal to the value obtained in carrying the first i-1 item into a backpack of capacity J. Obviously, taking the most value of the two is the best solution for loading the first I item into a backpack with a capacity of J.

With poj3624 practiced hand

First open two-dimensional DP array, direct hyper memory

The second time alternating with two one-dimensional arrays, memory is not super, but it takes more than 400 MS, and the code is ugly

1#include <iostream>2#include <string>3#include <sstream>4#include <vector>5 6 using namespacestd;7 8 9 intMain ()Ten { One     intN, M; ACIN >> N >>m; -     int*w = (int*)malloc(sizeof(int) * (n+1)); -     int*d = (int*)malloc(sizeof(int) * (n +1)); the  -     int*DP = (int*)(malloc(sizeof(int) * (m+1))); -     int*dp_t = (int*)(malloc(sizeof(int) * (M +1))); -  +Memset (DP,0,sizeof(int) * (M +1)); -  +      for(inti =1; I <= N; i++) A     { atCIN >> W[i] >>D[i]; -     } -  -      for(inti =1; I <= N; i++) -     { -memset (dp_t,0,sizeof(int) * (M +1)); in  -          for(intj =1; J <= M; J + +) to         { +             intx =Dp[j]; -             inty =0; the             if(J >=W[i]) *y = Dp[j-w[i]] +D[i]; $DP_T[J] = x < y?y:x;Panax Notoginseng              -         } the  +memcpy (DP, dp_t,sizeof(int) * (M +1)); A     } the  +cout << Dp[m] <<Endl; -  $     //System ("pause"); $}
View Code

Looked at someone else's code, only to find a more elegant processing method, the revised code as follows, time-consuming is reduced to less than 300ms

1#include <iostream>2#include <string>3#include <sstream>4#include <vector>5 6 using namespacestd;7 8 intdp[12900];9 intw[3410], d[3410];Ten  One intMain () A { -     intN, M; -CIN >> N >>m; the  -Memset (DP,0,sizeof(DP)); -  -      for(inti =1; I <= N; i++) +     { -CIN >> W[i] >>D[i]; +     } A  at      for(inti =1; I <= N; i++) -     { -  -          for(intj = m; J >0; j--) -         { -             intx =Dp[j]; in             inty =0; -             if(J >=W[i]) toy = Dp[j-w[i]] +D[i]; +DP[J] = x < y?y:x; -         } the     } *  $cout << Dp[m] <<Endl;Panax Notoginseng  -     //System ("pause"); the}
View Code

01 knapsack problem

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.