01 knapsack problem

Source: Internet
Author: User
Tags cmath

Backpack Nine talk about the bong

Http://love-oriented.com/pack/P01.html

Take Hdu 2602 as an example:

http://acm.hdu.edu.cn/showproblem.php?pid=2602

Details of the initialization

There are actually two different kinds of questions that we see in the knapsack problem of finding the best solution. Some of the topics require "just fill the backpack" when the optimal solution, and some of the problems are not required to fill the backpack. One way of distinguishing between the two methods of asking is to make a difference when initializing.

If this is the first method of asking for a full backpack, then at initialization the f[0] is set to-∞ except for the 0 other F[1..V], so that the resulting f[n] is the optimal solution for a packed backpack.

If it is not required to fill the backpack, but only want the price as large as possible, the initialization should be F[0..V] all set to 0.

Why is it? It can be understood that the initialized F-array is in fact the legal state when no item can be placed in the backpack. If the backpack just fills up, then only the capacity of 0 of the backpack may be worth 0 of nothing "exactly full", the other capacity of the backpack are not legal solution, belong to the undefined state, their values should be-∞. If the backpack does not have to be filled, then any capacity backpack has a legitimate solution "nothing", the value of this solution is 0, so the initial state of the value is all 0.

A constant optimization

There is a for v=v in the preceding pseudo-code: 1, the lower limit of this cycle can be improved.

Because only need the last F[v] value, backward push the previous item, in fact as long as know F[v-w[n]] can. And so on, to the J backpack, actually only need to know to f[v-sum{w[j. N]}], that is, the code in the

For I=1..N for    V=v. 0

can be changed into

For I=1..N    bound=max{v-sum{w[i. N]},c[i]} for    v=v. Bound

This is useful for V when it is relatively large.

Two-dimensional:

#include <iostream>#include<string>#include<cstring>#include<cstdlib>#include<cstdio>#include<cmath>#include<algorithm>#include<stack>using namespacestd;#defineMEM (A, B) memset (A,b,sizeof (a))#definePF printf#defineSF scanf#defineDebug printf ("!/m")#defineINF 1000#defineMAX (A, b) a>b?a:b#defineBlank pf ("\ n")#defineLL Long LongintDp[inf][inf];intCi[inf];intWi[inf];intMain () {intn,v,i,j,v,t; SF ("%d",&t);  while(t--) {SF ("%d%d",&n,&W); MEM (DP,0); MEM (CI,0); MEM (WI,0);  for(i =1; i<=n;i++) {SF ("%d",&Wi[i]); }                     for(i =1; i<=n;i++) {SF ("%d",&Ci[i]); }                     for(i =1; i<=n;i++)                    {                               for(v =0; v<=v;v++)//if a volume of 0 is not allowed, it can be set to 1 .                              {                                        if(ci[i]<=v) dp[i][v]= MAX (dp[i-1][v],dp[i-1][v-ci[i]]+Wi[i]); ElseDp[i][v]= dp[i-1][v]; }} PF ("%d\n", Dp[n][v]); }    return 0;}

One-dimensional:

Space complexity optimization, time complexity is basically unchanged

#include <iostream>#include<string>#include<cstring>#include<cstdlib>#include<cstdio>#include<cmath>#include<algorithm>#include<stack>using namespacestd;#defineMEM (A, B) memset (A,b,sizeof (a))#definePF printf#defineSF scanf#defineDebug printf ("!/m")#defineINF 1000#defineMAX (A, b) a>b?a:b#defineBlank pf ("\ n")#defineLL Long LongintDp[inf];intCi[inf];intWi[inf];intMain () {intn,v,i,j,v,t; SF ("%d",&t);  while(t--) {SF ("%d%d",&n,&V); MEM (DP,0); MEM (CI,0); MEM (WI,0);  for(i =1; i<=n;i++) {SF ("%d",&Wi[i]); }                     for(i =1; i<=n;i++) {SF ("%d",&Ci[i]); }                     for(i =1; i<=n;i++)                    {                               for(v = v;v>=ci[i];v--)//the volume can be 0{Dp[v]= MAX (dp[v],dp[v-ci[i]]+Wi[i]); }} PF ("%d\n", Dp[v]); }    return 0;}

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.