Multiple backpacks and determine if they can be filled (with 01 full backpack thought)

Source: Internet
Author: User

This is a problem in our homework, but also I think it is a very fun problem, is a bare multi-backpack, but it is only simple let me judge whether to fill. My first time in the tle, I thought the data of the homework problem is not very strong, simply stole a lazy enumeration of the number of the next selection, no binary optimization directly timed out, underestimated the teacher ~ so I added a binary optimization, experienced a bumpy before.

About this backpack knowledge, I want to say a little more, after all, the more basic things to strengthen understanding ah ...

First of all, say 01 and full backpack, these two are very basic backpack, the difference between them is from the current state transfer or from the previous state transfer, from the current state to represent but choose countless, is a complete backpack, and from the previous state and only choose one, is 01 backpack, If this is a two-dimensional, how do you choose regardless, but if it is a one-dimensional words, you must choose from the back, because we want to use the previous state, and in the judgment Dp[j-v[i]], this is exactly the previous state, if the election, it is just the current state, it is exactly the full backpack.

In fact, how to determine whether to fill, this we need a mark, I have seen a person's blog, he is using the-1 initialization state, when the state is transferred if the previous state is equal to 1, it can not be transferred, so the last is not-1 of the point is able to exactly fill the point, Judging the last point that the backpack capacity is 1 is the ability to fill up the judgment method. But I am more recommended is to initialize the method of-inf (dp[0] = 0), it avoids the criteria, so that they can judge whether he finally <=0 on it, the attention must be-inf, if the negative value is too small and finally added positive value is awkward ... In this way, I see a person's blog said that the last point has not been changed, I want to say that this is wrong, he must have been changed, must have become bigger, but still negative or negative infinity. In short, the method of judging is the mark method, how to mark depends on the topic request and the personal habit.

Then, I would like to say that the path of the backpack output, in fact, the program output, accustomed to ... This topic has never been seen, but I would like to mention it, you can define a path of a two-dimensional array, when the transition conditions to record, and finally inverted output (because we are taking), 01 backpack output a i--once, Full backpack until this position is empty i--(full backpack This is my conjecture, there is no strong proof, but the 01 backpack is right).

Finally, return to the point. Multi-backpack binary optimization, sub-case discussion, if the Num*cost >= backpack capacity as a complete backpack to deal with it, the reverse is turned into a binary optimization method, a number of items as an item, according to 01 backpack Way to do.

The code is as follows:

#include <cstdio>#include<queue>#include<stack>#include<iostream>#include<cstring>using namespacestd;#defineMAXN 2000010Long LongDp[maxn],w[maxn],num[maxn],all;intMain () {intt,n,m; scanf ("%d",&t);  while(t--) {scanf ("%d%lld",&n,&All );  for(inti =0; I < n; i++) {scanf ("%lld%lld",&num[i],&W[i]); }         for(inti =1; I <= all; i++) Dp[i] =-9999999; dp[0] =0;  for(inti =0; I < n; i++)        {            intTMP = num[i] * W[i];///why is that rough ?            if(TMP >=All ) {                 for(intj = W[i]; J <= All; J + +)///This is where the W[i] is written in TMP ...{Dp[j]= Max (Dp[j],dp[j-w[i]) +1); }            }            Else            {                intK =1; TMP=Num[i];  while(K <tmp) {                     for(intj = All; J >= K*w[i]; j--) Dp[j]= Max (Dp[j],dp[j-k*w[i]) +k); TMP-=K; K<<=1; }                 for(intj = All; J >= Tmp*w[i]; j--) Dp[j]= Max (Dp[j],dp[j-tmp*w[i]) +tmp); }        }        if(Dp[all] <0) printf ("false\n"); Elseprintf"true\n"); }    return 0;}

Multiple backpacks and determine if they can be filled (with 01 full backpack thought)

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.