Total Backpack--01 Backpack method number

Source: Internet
Author: User

These two days after 01 backpack study Complete Backpack This full backpack refers to the number of items unlimited, let oneself to install consider the state transfer F[i][j]=max (F[i-1][j]) has not selected the article I, F[i][j]=max (F[i][j-w[i]+v[i]); Select an item from the I key the obvious state is that it is transferred by comparing the optimal solution of the current item to the current one and taking the optimal solutions of the item. Of course the target can be maxf[n][j] or f[n][m] Both of these are just the former can determine whether the backpack can be filled.

Here is the two-dimensional code:

#include <ctime>#include<iostream>#include<cstdio>#include<algorithm>#include<cstdio>#include<iomanip>#include<map>#include<queue>#include<stack>#include<cstring>#include<string>#include<vector>using namespaceStd;inlineLong LongRead () {Long Longx=0, f=1;CharCh=GetChar ();  while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();}  while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; ch=GetChar ();} returnx*F;}Const Long Longmaxn=10001;Long LongF[MAXN][MAXN],W[MAXN],V[MAXN];Long Longn,m;intMain () {//freopen ("1.in", "R", stdin);M=read (); n=read ();  for(Long LongI=1; i<=n;i++) {V[i]=read (); w[i]=read (); }     for(Long LongI=1; i<=n;i++)         for(Long Longj=1; j<=m;j++) {F[i][j]=max (f[i][j],f[i-1][j]); if(J>=w[i]) F[i][j]=max (f[i][j],f[i][j-w[i]]+V[i]); } printf ("%lld\n", F[n][m]); return 0;}
View Code

And one-dimensional complete backpack is like that, put F[j]=max (F[j-w[i]]+v[i]), the positive sequence to select because it can be updated on the first article I and then update this means that an item has countless pieces. Not really.

The following is a one-dimensional code:

#include <ctime>#include<iostream>#include<cstdio>#include<algorithm>#include<cstdio>#include<iomanip>#include<map>#include<queue>#include<stack>#include<cstring>#include<string>#include<vector>using namespaceStd;inlineintRead () {intx=0, f=1;CharCh=GetChar ();  while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();}  while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; ch=GetChar ();} returnx*F;}Const intmaxn=10001;intF[MAXN],W[MAXN],V[MAXN];intn,m;intMain () {//freopen ("1.in", "R", stdin);M=read (); n=read ();  for(intI=1; i<=n;i++) {V[i]=read (); w[i]=read (); }     for(intI=1; i<=n;i++)    {         for(intj=w[i];j<=m;j++) {F[j]=max (f[j],f[j-w[i]]+V[i]); }} cout<<f[m]<<Endl; return 0;}
View Code

also made a 01 backpack method number, this is relatively simple personal think method number words with the following Luo gu example:

To find out the number of all backpacks, so that the help of seniors, not ah at that time feel particularly difficult to do the problem without extrapolate ability to feel that they do not, spirituality is not enough. It's obviously a 01-pack variant, so just change the transfer equation of the 01-pack a little bit,

F[j]=f[j]+f[j-w[i]]; initialize f[0]=1; This problem can be easily dropped with a few small details.

Code:

#include <ctime>#include<iostream>#include<cstdio>#include<algorithm>#include<cstdio>#include<iomanip>#include<map>#include<queue>#include<stack>#include<cstring>#include<string>#include<vector>using namespaceStd;inlineintRead () {intx=0, f=1;CharCh=GetChar ();  while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();}  while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; ch=GetChar ();} returnx*F;}Const intmaxn=1002;inta[7]={0,1,2,3,5,Ten, -};intw[maxn],f[maxn],n=0, m=0, ans=0;intMain () {//freopen ("1.in", "R", stdin);     for(intI=1; i<=6; i++)    {        intx; X=read ();  for(intj=1; j<=x;j++) {w[++n]=A[i]; M+=A[i]; }} f[0]=1;  for(intI=1; i<=n;i++)    {         for(intj=m;j>=w[i];j--) {F[j]=f[j]+f[j-w[i]];//How many schemes are selected from the I number and for J        }    }     for(intI=1; i<=m;i++)    {        if(f[i]!=0) ans++; } printf ("total=%d\n", ans); return 0;}
View Code

Total Backpack--01 Backpack method number

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.