Classic Backpack Series Questions

Source: Internet
Author: User

Knapsack problem I
Question Description

There is a backpack volume of V and n items, and gives each item a volume. Request from n items, any number of packs into the backpack, so that the remaining space of the backpack is minimal.

Input
The first row of two positive integers V and n, respectively, the volume of the backpack and the number of items to be installed; The second line consists of n positive integers representing the volume of n items and a space between 22.
Output
A number that represents the minimum amount of space left in the backpack
Input example
24 6
8 3 12 7 9 7
Output example
0
Other Notes
Data range: 0<v≤20000,0<n≤30
View Code

Knapsack Problem II
Question Description

It's still a backpack problem ... Typical 0-1 backpack! Yue: Today there are n items, the first volume of v[i], the value of W[i], the volume of the backpack is C. In the case of volume without exceeding the volume of the premise, the backpack can be loaded with the maximum value of the item value.

Input
First line: two integers n and C;
Second line ~ n+1 line: two integers per line VI with WI, there is a space separated.
Output
A number that represents the maximum value that can be obtained in a backpack.
Input example
2 10
1 1
2 2
Output example
3
1#include <iostream>2 using namespacestd;3 inta[10001]={},b[10001]={},TEMP1,TEMP2;//B[i] As the previous line, A[i] for this line4 intMain ()5 {6     intn,c;7     inti,j;8Cin>>n>>C;9      for(i=1; i<=n;i++)Ten     { Onecin>>temp1>>temp2;//input A          for(j=1; j<=c;j++)//DP -         { -             if(J&LT;TEMP1) a[j]=B[j]; the             Else{A[j]=max (b[j-temp1]+temp2,b[j]);} -         } -          for(j=1; j<=c;j++) b[j]=A[j]; -     } +cout<<B[c]; -}
View Code

knapsack problem iii
 
question description

     Backpack problem, ancient classics. Yue: There are many n kinds of items, the volume of the first I is v[i], the value of w[i], backpack volume C. Find the maximum value of the item in the backpack under the premise of volume not exceeding volume.

input
first row: two integers  n  and  c 
Second line ~ n+1 line: two integers per line VI and W I, there is a space separating.
output
A number that indicates the maximum value that can be obtained from the item in the backpack.  
Input Example
4 1000
1 1000
2&nbs p;1231
3 1232
4 1010
Output Show example
1000000
Additional instructions
data range:1<=n<=100 1<=vi,wi<=100; 1<=c<=10000; 
1#include <iostream>2 using namespacestd;3 inta[2][10001]={},temp1,temp2;4 intMain ()5 {6     intn,c;7     inti,j;8Cin>>n>>C;9      for(i=1; i<=n;i++)Ten     { OneCin>>temp1>>Temp2; A          for(j=1; j<=c;j++) -         { -             if(J&LT;TEMP1) a[1][j]=a[0][j]; the             Else{a[1][j]=max (a[1][j-temp1]+temp2,a[0][j]);} -         } -          for(j=1; j<=c;j++) a[0][j]=a[1][j]; -     } +cout<<a[1][c]; -}
View Code

Knapsack problem Ⅳ
Question Description

There are n weight and value items for WI and VI respectively. Select items from these items that have a total weight of not more than W, and ask for the maximum value of the sum of the values in all selection scenarios.

Input
Three lines, the first row contains two positive integers n and W, the second row contains n positive integers, which in turn represent the weight of I items, and the third row contains n positive integers, which in turn represents the value of each item. The number 22 in the same row is separated by a space.
Output
A number that represents the maximum value of the sum of values.
Input example
4 5
2 1 3 2
3 2 4 2
Output example
7
Other Notes
Data range: 1<=n,vi<=100,1<=w<=10^9,1<=wi<=10^7.
1#include <iostream>2 using namespacestd;3 inta[2][10005]={},t1[101],t2[101];4 intMain ()5 {6     intn,w;7Cin>>n>>W;8      for(intI=1; i<=n;i++) scanf ("%d",&t1[i]);9      for(intI=1; i<=n;i++) scanf ("%d",&t2[i]);Ten      for(intI=1; i<=n;i++) One     { A          for(intj=10001; j>=1; j--) -         { -a[1][j]=a[0][j]; the             if(J<t2[i]) Break; -             if(j-t2[i]==0) -             { -                 if(a[1][j]==0) a[1][j]=T1[i]; +                 Elsea[1][j]=min (a[1][j],t1[i]); -             } +             Else if(a[0][j-t2[i]]!=0) A             { at                 if(!a[1][J]) a[1][j]=a[0][j-t2[i]]+T1[i]; -                 Elsea[1][j]=min (a[1][j],a[0][j-t2[i]]+t1[i]); -             } -         } -          for(intj=1; j<=10001; j + +) a[0][j]=a[1][j]; -     } in     intans=0; -      for(intI=1; i<=10001; i++)if(a[1][i]<=w&&a[1][i]!=0) ans=i; tocout<<ans; +}
View Code

Classic Backpack Series Questions

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.