0/1 knapsack problem

Source: Internet
Author: User

Code Listing 1:

1#include <iostream>2#include <vector>3 using namespacestd;4 Const intmin=0x80000000;5 Const intn=3;//Number of items6 Const intv=5;//Backpack Capacity7 intf[n+1][v+1];8 9 intPackage (int*w,int*c,intNintV);Ten voidMainintargcChar*argv[]) One { A  intw[4]={0,7,5,8};//Item Weights -  intc[4]={0,2,3,4};//Item Size -  intresult=Package (w,c,n,v); the  if(result>0) -  { -cout<<Endl; -cout<<"The opt value:"<<result<<Endl; +   inti=n,j=V; -    while(i) +   { A    if(f[i][j]== (f[i-1][j-c[i]]+W[i])) at    { -cout<<i<<":"<<"w="<<W[i]<<", c="<<C[i]<<Endl; -j-=C[i]; -    } -i--; -   } in  } -  Else tocout<<"can not find the OPT value"<<Endl; +  return; - } the  * intPackage (int*w,int*c,intNintV) $ {Panax Notoginseng  inti,j; -Memset (F,0,sizeof(f));//initialized to 0 the  +   for(i=0; i<=n;i++) A   for(j=1; j<=v;j++)//This step is to resolve whether the pack capacity is exactly met, theF[i][j]=min;//if "just" satisfies the backpack capacity, that is, full pack, then add this step, if you do not need "exactly", then initialized to 0 +      -   for(i=1; i<=n;i++) $    for(j=c[i];j<=v;j++) $   { -F[i][j]= (f[i-1][j]>f[i-1][j-c[i]]+w[i])? f[i-1][J]:(f[i-1][j-c[i]]+w[i]); -cout<<"f["<<i<<"]["<<j<<"]="<<f[i][j]<<Endl; the   } -  returnF[n][v];Wuyi}
View Code

Code Listing 2:

1 Code 22 3#include <iostream>4#include <vector>5 using namespacestd;6 Const intmin=0x80000000;7 Const intn=3;//Number of items8 Const intv=5;//Backpack Capacity9 intf[v+1];Ten  One intPackage (int*w,int*c,intNintV); A voidMainintargcChar*argv[]) - { -  intw[4]={0,7,5,8};//Item Weights the  intc[4]={0,2,3,4};//Item Size -  intresult=Package (w,c,n,v); -  if(result>0) -  { +cout<<Endl; -cout<<"The opt value:"<<result<<Endl; +  } A  Else atcout<<"can not find the OPT value"<<Endl; -  return; - } -  - intPackage (int*w,int*c,intNintV) - { in  inti,j; -Memset (F,0,sizeof(f));//initialized to 0 to  +   for(i=1; i<=v;i++)//This step is to resolve whether the pack capacity is exactly met, -F[i]=min;//if "just" satisfies the backpack capacity, that is, full pack, then add this step, if you do not need "exactly", then initialized to 0 the      *   for(i=1; i<=n;i++) $    for(j=v;j>=c[i];j--)//Note that there is a difference in order between this and the solution, to clarify the reasonPanax Notoginseng   { -F[j]= (F[j]>f[j-c[i]]+w[i])? F[J]:(f[j-c[i]]+w[i]); thecout<<"f["<<i<<"]["<<j<<"]="<<f[j]<<Endl; +   } A  returnF[v]; the}
View Code

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