Ask the big God a deformed knapsack problem

Source: Internet
Author: User
There are n kinds of goods and a backpack with a capacity of V. There is also a threshold T.
(i) Items with a maximum of n[i] pieces available, each cost is c[i], the value is w[i].
The solution of which items are loaded into the backpack allows the sum of these items not to exceed the backpack capacity and the sum of the values is greater than and closest to T.

Complete Backpack Code included

/**
* Backpack problem Description: A backpack that withstands a maximum weight of W, now has n items, each item weighs t, and each item has a value of V.
* To make the backpack the biggest weight (but not more than W), but also need the most value of the backpack.
* Idea: Define a two-dimensional array, one-dimensional for the number of items (for each item), two-dimensional is the weight (not exceeding the maximum, here is 10), the following array a,
* Theory of dynamic programming, Max (OPT (i-1,w), wi+opt (I-1,W-WI)),
*opt (I-1,W-WI) refers to the previous optimal solution
*/
This is what I wrote based on the principle of dynamic programming.
Max (opt (i-1,w), wi+opt (I-1,W-WI))
The backpack can fit the maximum weight
$w = 10;
Here are four items, the weight of each item
$DX =array (3,4,5);
The value of each item
$QZ =array (4,5,6);
Define an array
$a =array ();
Initialization
for ($i =0; $i <=10; $i + +) {$a [0][$i]=0;}
for ($j =0; $j <=3; $j + +) {$a [$j][0]=0;}
Opt (i-1,w), wi+opt (I-1,W-WI)
for ($j =1; $j <=3; $j + +) {
for ($i =1; $i <=10; $i + +) {
$a [$j] [$i]= $a [$j -1][$i];
Not greater than the largest w=10
if ($dx [$j -1]<= $w) {
if (!isset ($a [$j -1][$i-$DX [$j-1]]) continue;
Wi+opt (I-1,WI)
$tmp = $a [$j -1][$i-$dx [$j -1]]+ $QZ [$j-1];
Opt (i-1,w), wi+opt (i-1,w-wi) = Compare
if ($tmp > $a [$j] [$i]) {
$a [$j] [$i]= $tmp;
}
}
}
}
To print this array, the value of the right-hand corner of the output is maximum value.
for ($j =0; $j <=3; $j + +) {
for ($i =0; $i <=10; $i + +) {
echo $a [$j] [$i]. " ";
} echo "
";
}


?>


Reply to discussion (solution)

What problems have you encountered? Your results should be right.
But I like to write this.

 $w = ten; $ar = Array (' W ' = 3, ' V ' = 4), Array (' w ' = 4, ' V ' = 5),  Array (' W ' = 5, ' V ' = 6), foreach ($ar as $k + $v) {$v [' k '] [] = $k; $res [] = $v;} $p = 0;for (; $p 
  
    $v) {if (In_array ($i, $res [$p] [' K '])) Continu    E if ($r [' W '] + $v [' W '] <= $w) {$res [] = Array (' w ' = = $r [' W '] + $v [' W '], ' v ' = = $r [' V '] + $v [' V    '], ' k ' = = Array_merge ($r [' K '], Array ($i)),); }}}foreach ($res as $v) $t [] = $v [' V '];array_multisort ($t, Sort_desc, $res);
 Print_r ($res); 
  
Array ([0] = = Array ([w] = 9 [v] = = [K] = = array            ([0] = 1 [1] = 2)) [1] = = Array (                    [W] = 9 [v] = [k] = = Array ([0] = 2 [1] = 1)) [2] = = Array ([w] = 8 [v] =                > [k] = = Array ([0] = 0 [1] = 2                )) [3] = = Array ([w] = 8 [v] = [k] = = array        ([0] = 2 [1] = 0)) [4] = = Array ([w] = 7 [v] = 9 [k] = = Array ([0]                 = 0   [1] = 1)) [5] = = Array ([w] = 7 [v] = 9        [K] = = Array ([0] = 1 [1] = 0)                    ) [6] = = Array ([w] = 5 [v] = 6 [k] = = Array ( [0] = 2)) [7] = = Array ([w] = 4 [v] = 5 [k] = = Array ([0] = 1)) [8] = =                    Array ([w] = 3 [v] = 4 [k] = = Array ( [0] = 0)))

Now this is the greatest value of a backpack full of w=10.

I was trying to find out: In the case where the backpack is as full as possible, the total value is close to a given threshold T combination

$f = 9;print_r (Array_filter ($res, function ($a) use ($f) {return $a [' V '] > 0.9* $f && $a [' V ']<1.1* $f;}]);


Too strong, thank the moderator adult! Very cow B, the result was completely reached

This algorithm you write too concise, a bit do not understand, ask you again, how to filter out the repetition of the combination ah? For example 0 3 6, 3 0 6,6 0 3

..... array_multisort ($t, Sort_desc, $res); Sort (existing) foreach ($res as $i = $v) if (Isset ($res [$i-1]) && Array_diff ($res [$i -1][' K '], $v [' K ']) unset ($res [$i]); Go to heavy $res = Array_values ($res); Normalized


My algorithm doesn't exactly follow a dynamic plan.
It's a record of all the possible combinations, and finally a sort check.

Thank you so much, thank you for your advice.

  • 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.