0-1 Backpack

Source: Internet
Author: User

0-1 Backpack:

0-1 Backpack is the basis of knapsack problem, it derived from the knapsack problem roughly with the same idea.

And since it belongs to the dynamic programming problem, its method lies in two steps: 1) define the state: Ans[i][t] represents the maximum value of the first item selected in the capacity of T, only 2) to determine the state transition equation: ans[i][t]=max{ans[i-1][t],ans[i][t-t [I]] +v[i]} (the article I and not take two cases of comparison) method of compressing space: with an array to record the maximum value, in the selection of the choice of reverse, so exactly can be overwritten, the specific understanding can be each time the selection process output, followed by the results analysis will be better.
1#include <iostream>2 using namespacestd;3 Const intn=302;//Quantity4 5 intT[n];//Price6 intV[n];//value7 8 intMain ()9 {Ten     intT,num;//Total capacity and number of items One      while(cin>>t>>num) A     { -         inti,j; -          for(i=1; i<=num;i++) theCin>>v[i]>>T[i]; -         int*ans=New int[t+1](); -          for(i=1; i<=num;i++) -         { +             intm; -              for(m=t;m>=t[i];m--)//The key!! Write in reverse!  +             { A                 if(ans[m]< (ans[m-t[i]]+V[i])) atans[m]=ans[m-t[i]]+V[i]; -             } -         } -cout<<ans[t]<<"\ n"; -         Delete[]ans; -     } in     return 0; -}

Variant 1: Output each item selected

Procedure: At the time of judging with an array to mark, indicating in this volume I item to take. The final output is then marked with the item's ordinal output (so the output is reversed)

1#include <iostream>2#include <string.h>3 using namespacestd;4 Const intn=302;//Quantity5 Const intwhole=20000;6 intT[n];//Price7 intV[n];//value8 BOOLCheck[n][whole];//mark the item to take no9 intMain ()Ten { One     intT,num;//Total capacity and number of items A      while(cin>>t>>num) -     { -         inti; the          for(i=1; i<=num;i++) -Cin>>v[i]>>T[i]; -memset (check,false,sizeof(check)); -         int*ans=New int[t+1](); +          for(i=1; i<=num;i++) -         { +             intm; A              for(m=t;m>=t[i];m--)//The key!! Write in reverse!  at             { -                 if(ans[m]< (ans[m-t[i]]+V[i])) -                 {    -ans[m]=ans[m-t[i]]+V[i]; -check[i][m]=true;//under the capacity of M, the article I items to take -                     //cout<<ans[m]<< "" <<check[i][m]<<endl; in                 }    -             } to         } +cout<<ans[t]<<"\ n"; -  the          while(num>0) *         { $             if(Check[num][t])//under the capacity of M, the article I items to takePanax Notoginseng             { -cout<<num<<" "; theT-=t[num];//after removal, the capacity reduces the corresponding cost +             } Anum--; the         } +cout<<"\ n"; -  $         Delete[]ans; $     } -     return 0; -}

Variant 2: The capacity is exactly out of use

Key: ans[] initialization sets Ans[0] to 0, others are a very small negative value.

1#include <iostream>2 using namespacestd;3 Const intn=302;//Quantity4 5 intT[n];//Price6 intV[n];//value7 8 intMain ()9 {Ten     intT,num;//Total capacity and number of items One      while(cin>>t>>num) A     { -         inti; -          for(i=1; i<=num;i++) theCin>>v[i]>>T[i]; -         int*ans=New int[t+1](); -          for(i=1; i<=t;i++)//key point, Ans[0] is 0, others are very small negative values -         { +ans[i]=-99999; -         } +          for(i=1; i<=num;i++) A         { at             intm; -              for(m=t;m>=t[i];m--) -             { -                 if(ans[m]< (Ans[m-t[i]]+v[i]))//only ans[0] makes sense, which means that the capacity is exactly exhausted. All other cases are a small value.  -ans[m]=ans[m-t[i]]+V[i]; -             } in         } -cout<<ans[t]<<"\ n"; to         Delete[]ans; +     } -     return 0; the}

0-1 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.