Multiple knapsack Problem II

Source: Internet
Author: User

Multiple knapsack Problem II
The total volume is M, the volume of each small item is a[i], the quantity of each small item is b[i], the value of each small item is c[i]

Maximum value that can be obtained by placing the largest item in the backpack

Very similar to the previous

The last question volume is value, and the value here is defined separately.

State transition equation

Don't put A[i]

F[I][J] =f[i-1][j]

Put A[j]

You can put more than one set to K,

k = min (J/a[i],b[i])

F[I][J] = F[i-1][j-ki*a[i]] + ki*c[i] 0<=ki<=k take maximum value

Full Backpack problem time:0<=ki*a[i]<=m

 Public classSolution { Public intBackPack (intMint[] A,int[] B,int[] C) {//Write your code here        int[] P =New int[M+1];//P[i][j] Maximum value of the first I items placed in J space         for(inti = 0;i< a.length; i++){             for(intj = m;j>=1;j--){                if(j>=A[i]) {                    intk = J/a[i];//the maximum number of items can be k, but the maximum limit is b[i]K = Math.min (K,b[i]);//take the minimum value                     while(k>=0){                        if(j>=a[i]*k) {P[j]=math.max (P[j], P[j-k*a[i]] + k*C[i]); } k--; }                }                                    ElseP[j]=Math.max (P[j],p[j]); }        }        returnP[m]; }    /*** Multiple knapsack problem * Total volume is m, each small item volume is a[i], each small item quantity is b[i] * *@paramM:an integer m denotes the size of a backpack *@parama:given n items with size A[i] 0 starting with a is *@return: The maximum size*/     Public intBackPack1 (intMint[] A,int[] B,int[] C) {//Write your code here        int[] P =New int[A.length+1] [M+1];//P[i][j] Maximum value of the first I items placed in J space         for(inti = 0;i< a.length; i++){             for(intj = m;j>=1;j--){                if(j>=A[i]) {                    intk = J/a[i];//the maximum number of items can be k, but the maximum limit is b[i]K = Math.min (K,b[i]);//take the minimum value                     while(k>=0){                        if(j>=a[i]*k) {P[i+1][J] =math.max (P[i+1][j], P[i][j-k*a[i]] + k*B[i]); } k--; }                }                                    ElseP[i+1][J] = Math.max (p[i][j],p[i+1][j]); }        }        returnP[a.length][m]; }     Public Static voidMain (string[] args) {intm = 10;//;//        int[] A=New int[]{1,2,3,4}; int[] b=New int[]{2,3,1,4}; int[] c=New int[]{2,13,4,2}; intsum =Newsolution (). BackPack (M, a,b,c);    SYSTEM.OUT.PRINTLN (sum); }}

10:45

100:55

Multiple knapsack Problem II

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.