Detachable knapsack problem (a greedy algorithm)

description:

       There are different values ​​of n v [i] and the weight w [i] of the object (if the object is selected w [i] to obtain a value v [i]).

  Do you have a container to pick them. You can according to their own needs and divide them into pieces of any size. The object can be picked up maximum weight given as w. Calculate the maximum you can get.

 

Input:

       The first line of the input n W (0 <= n <= 1000) (0 <= W <= 10000)

       The second row input value of n items (0 <= v [i] <= 10000)

  The third line of the input mass of n items (0 <= w [i] <= 10000)

Output:

       Maximum value, rounded to three decimal places.

 

analysis:

  This problem with the traditional 0-1 knapsack problem is different in this problem can be divided into any part of the body, so we can use greedy algorithm to solve problems according to cost (value / quality) goods, in turn objects into the backpack according to the level of cost-effective when an object can not be completely into the backpack, the total value = value + backpack completely into the remaining space of the article should be placed next * backpack cost items.

 

Code:

#include <the iostream> 
#include <algorithm> 
#include <The iomanip>
 the using  namespace STD; 

// needs a structure, the cost, weight and value can be found.
// do a sort needs to be sorted in the end by the high cost, weight and sorting process (value) corresponds to the 

typedef struct {
     Double AVER;
     Double W;
     Double V; 
} Knapsack; 

BOOL CMP (Knapsack A, B Knapsack) 
{ 
    return a.aver> b.aver; 
} 
int main () 
{ 
    Knapsack Arrays [ 1009 ];
     int n-;
     Double m;
    double V = 0;
    cin >> n >> m;
    for (int i = 0; i < n; i++)
        cin >> arrays[i].v;
    for (int i = 0; i < n; i++)
        cin >> arrays[i].w;


    //求性价比
    for (int i = 0; i < n; i++)
    {
        arrays[i].aver = arrays[i].v / arrays[i].w;
        //cout << arrays[i].aver << endl;
    }

    // effective sorting 
    Sort (Arrays, Arrays + n-, CMP); 
  
    int SUM = 0 ;
     for ( int I = 0 ; I <n-; I ++)   // when the backpack can hold all items directly outputs the value of all the items sum 
    { 
        sUM + = Arrays [I] .W; 
    } 
    IF (sUM < m) 
    { 
        for ( int J = 0 ; J <n-; J ++ ) 
            V + = Arrays [J] .v;
           // V = Floor ( 1000.0 * V) / 1000.0; 
        COUT << setiosflags (iOS :: Fixed ) << setPrecision (. 3 ) V << << endl;
         return  0 ; 
    } 
    // should be cost-effective by the order, by the capacity to select the items loaded 

    for ( int I = 0 ; I <n-; I ++ ) {
         IF (Arrays [I]. W <= m) 
        { 
            V = V + Arrays [I] .v; 
            m = m - Arrays [I] .W; 
        } 
        the else { // directly added to the remaining m 
            V m = V * + Arrays [I ] .aver; 
            m = 0 ; 
        } 
        IF (m == 0)
            break;
    }
    //V = floor(V * 1000.0) / 1000.0;
    cout << setiosflags(ios::fixed) << setprecision(3) << V << endl;
    return 0;
}
 

 

Guess you like

Origin www.cnblogs.com/yichenxing/p/11106287.html