The backpack.

Source: Internet
Author: User

The first is free commentary: The knapsack problem these days

0 1 Package: reverse order. The result is obtained from the previous set of elements that were brushed past.

1 voidZeroonepack (intValintVol)2 {3 4     inti;5      for(i=v;i>=vol;i--)6     {7         if(dp[i-vol]+val>Dp[i])8         {9Dp[i] = dp[i-vol]+Val;Ten         } One     } A}
01 Backpack

Full package: when initializing. This is a good way to-inf one of the const int inf = 1<<28-1;. Write at the top of the

The result conforms to the value of the re-attachment. Dp[0]=0 is common.

Principle: It is clear that this value should be small or large enough. can cause this result to be filtered out.

This can be used to do at most and at least the problem. How many questions have to be put in.

My idea was to make it two-dimensional and then at least go up. Search down at most. Already have to put how much directly in this layer of search.

Full backpack: order. Note that this complexity is only n*v. Because it is not an item brush a layer of circulation but an item brush a layer of circulation. And can not use the principle of 01 package to explain simply.

1 voidZeroonepack (intValintVol)2 {3 4     inti;5      for(i=vol;i<=v;i+)6     {7         if(dp[i-vol]+val>Dp[i])8         {9Dp[i] = dp[i-vol]+Val;Ten         } One     } A}
Full Backpack

Multiple backpacks: If you are using a fully-wrapped brush layer method. It is not good to know how many of your numbers are used to maintain the number!. So in the case of complete package vol*num>=v that is, you can place it in this range in random quantities. (Of course, pull.) Must be optimal. And if the value is less than 01 packets. However, the bin is optimized for 01 more packages. Is this time a sequence or a reverse order? is in order. Understanding can be used in 01 bags to go inside. The wrong way to understand the full package can be well explained here. So use a variable k=1 to do it. I'm going to have to do 01 num at the end.

1 voidMulpack (intValintvol.,intnum)2 {  3     if(val*num>=aver)4     {  5 Compack (Val,vol); 6         return; 7     }  8    9     intk=1; Ten      while(k<num) One     {   AZeroonepack (k*val,k*vol.);  -   -num-=K;  theK *=2;  -     } -Zeroonepack (num*val,num*vol.);  -}
Multiple Backpack

Hybrid Backpack: This backpack is not difficult, as long as the incoming method is correct. and pay attention to the full package problem.

1 intMaxintAintb)2 {3     returnA>b?a:b;4 }5 voidOnezeropack (intValintVol)6 {7     intJ;8      for(j=v;j>=vol;j--)9     {TenDP[J] = max (dp[j],dp[j-vol]+val); One     } A } - voidCompack (intValintVol) - { the     intJ; -      for(j=vol;j<=v;j++) -     { -DP[J] = max (dp[j],dp[j-vol]+val); +     } - } + voidMulpack (intValintvol.,intnum) A { at     if(val*num>=V) -     { - Compack (val,vol); -         return; -     } -     intK =1; in      while(k<num) -     { to Onezeropack (val,vol); +num-=K; -K *=2; the     } * Onezeropack (val,vol); $}
the above three species together.

Two-dimensional fees: Plainly, add an array. One more layer of loops. In fact, often with the full package and the like together. This is the time to be aware of initialization. In addition, the two-dimensional costs often occur in the number of restrictions.

The specified number of M must be placed. This time is often the problem of full package. The state Division also has to pay attention to its meaning changes. In fact, if there is no such problem. What's the point of that number limit? Yes, too. In fact, we are in order to meet the conditions to use the two-dimensional. Then the state equation uses the source method.

The backpack.

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.