Ytu 2335:0-1 knapsack problem __ytu

Source: Internet
Author: User
Tags first row
2335:0-1 knapsack problemTime limit: 1 Sec Memory limit: 128 MB
Submitted: 15 Settlement: 12
Topic Description

A function of searching a subset space tree by backtracking method is designed. The parameters of the function include the necessary function of the nodal feasibility decision function and the upper bound function, and use this function to solve the 0-1 knapsack problem. The 0-1 knapsack problem is described as follows: given n kinds of items and a backpack. The weight of the item I is WI, its value is VI, the capacity of the backpack is C. How to choose the items to be loaded into the backpack, so that the total value of the goods loaded into the backpack is the largest? When choosing items to pack, there are only 2 options for each item I have, namely, a backpack or no backpack. The goods I can not be loaded into the backpack several times, also can not only be loaded into part of the goods I. input

The first row has 2 positive integers n and C. n is the number of items, C is the capacity of the backpack. The next 1 lines have n positive integers that indicate the value of the item. In line 3rd, there are n positive integers that represent the weight of the item.

Output

The maximum value and optimal loading scheme output of the loaded knapsack items are calculated. The first line output is: Optimal value is

Sample Input

5
6 3 5 4 6 2 2 6 5 4


Sample Output
Optimal value is
1 1 0 0 1
lost in the valley of the birds, flying alone in this huge world, but do not know where to fly to the side ...
#include <iostream> #include <stdio.h> using namespace std;
int ab,c,n,ji[200],h[200];
    struct Bag {int w;
int h;
} b[200];                 void Digui (int d,int s,int SC)//d for the first few items, for the current value, SC for the current occupied mass {if (sc>c) return;
        If current quality is greater than capacity return if (d==n)//If all items are inspected {if (ab<s)//current value is less than maximum value
            {ab=s;
        for (int i=0; i<d; i++)//recording the information at this time on the placement of items h[i]=ji[i];    The else for (int i=0; i<2; i++) {if (i==0) Ji[d]=0,digui (D+1,S,SC); Do not put the current item else Ji[d]=1,digui (D+1,S+B[D].W,SC+B[D].H);
    Put into current item} int main () {cin>>n>>c;            for (int i=0; i<n; i++) cin>>b[i].w;            Enter value for (int i=0; i<n; i++) cin>>b[i].h;               Input weight Digui (0,0,0);
    recursive function printf ("Optimal value is\n%d\n", AB); for (int i=0; i<n; i++) printf (i!=n-1?) %d ":"%d\n ", H[i]);
return 0;
 }

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.