Think back to the algorithm, do not go into non-algorithm experts

Source: Internet
Author: User
Do not go back to the algorithm! At the end of this post, xuzuning edited n pieces of a given item from 2011-06-. Their weights are w [0], w [1],… W [n-1]: the value of an item is v [0], v [1],… V [n-1], there is another backpack, which can accommodate the total weight of the backtracking algorithm, rather than the algorithm experts!
At the end of this post, xuzuning edited n pieces of a given item at 14:40:16. Their weights are w [0], w [1],… W [n-1]: the value of an item is v [0], v [1],… V [n-1], and another backpack, which can accommodate a total weight of w. Design an item selection scheme, which requires that the total weight of the items selected from the n items should not exceed the capacity of the backpack w, so that the total value of the selected items is the largest.

This is a common backtracing algorithm. who can use php to write it!


Note: replies irrelevant to algorithms will be deleted without mercy! Moderator shares:
------ Solution --------------------
This question takes at least one hour. let's talk about the ideas and let others do it.

1. sort the array of w and select items smaller than w (assign an array p ),
2. calculate the Cartesian product array for the p array, and select a subset of the items in all child sets and a subset smaller than w weight (assign an array r ),
3. convert the items in each sub-set in the r array to the corresponding v values, and sum them separately (assign values to the wv array ),
4. sort the wv array and obtain the key for the large value as the result.
------ Solution --------------------

$ M = 15;
$ Arr = array (), array (); // The first value is the price; the second value is weight.
Function Combination ($ arr, $ size = 1 ){
$ Len = count ($ arr );
$ Max = pow (2, $ len)-pow (2, $ len-$ size );
$ Min = pow (2, $ size)-1;
$ R_arr = array ();
For ($ I = $ min; $ I <= $ max; $ I ++ ){
$ T_arr = array ();
For ($ j = 0, $ k = 0; $ j <$ len; $ j ++ ){
$ A = pow (2, $ j );
$ T = $ I & $;
If ($ t = $ ){
$ T_arr [] = $ arr [$ j];
}
}
If (count ($ t_arr) ==$ size ){
$ R_arr [] = $ t_arr;
}
}
Return $ r_arr;
}
$ Num = count ($ arr );
For ($ I = 1; $ I <= $ num; $ I ++ ){
$ _ Tt = Combination ($ arr, $ I );
$ Num_tt = count ($ _ tt );
For ($ j = 0; $ j <$ num_tt; $ j ++ ){
$ _ T [] = $ _ tt [$ j];
}
} // Locate the possible situation
Function check_m ($ arr, $ m, $ jk = 1) {// $ arr indicates the array to be judged. $ m indicates the weight. $ jk indicates whether the weight or price is determined.
$ Num_t = count ($ arr );
For ($ I = 0; $ I <$ num_t; $ I ++ ){
$ Num_ti = count ($ arr [$ I]);
$ As = 0;
For ($ j = 0; $ j <$ num_ti; $ j ++ ){
$ As + = $ arr [$ I] [$ j] [$ jk];
}
If ($ as <= $ m ){
$ _ R [] = $ arr [$ I];
}
}
Return $ _ r;
}


Function check_max ($ arr ){
$ Ms = 0;
$ Num_t = count ($ arr );
For ($ I = 0; $ I <$ num_t; $ I ++ ){
$ Num_ti = count ($ arr [$ I]);
$ As = 0;
For ($ j = 0; $ j <$ num_ti; $ j ++ ){
$ As + = $ arr [$ I] [$ j] [0];
}
If ($ as >=$ ms ){
$ _ R = $ arr [$ I];
}
$ Ms = $;
}
Return $ _ r;
}
$ _ Rr = check_m ($ _ t, $ m, 1 );
$ _ R = check_max ($ _ rr );
Echo"
";
print_r($_r);
echo "
";
?>

------ Solution --------------------
This post was last edited by xuzuning at 14:01:34
Class Backtracking {
Private $ c = 0; // backpack capacity
Private $ n = 0; // Number of items
Private $ w = array (); // array of item weights
Private $ p = array (); // array of item values
Private $ cw = 0; // current weight
Private $ cp = 0; // current value
Private $ bestp = 0; // The current optimal value
Private $ d; // unit weight value
Private $ st = array ();

Function _ construct ($ w, $ p, $ c ){
$ This-> w = $ w;
$ This-> p = $ p;
$ This-> c = $ c;
$ This-> n = count ($ w );

$ This-> d = array_map (array ($ this, 'calculation'), $ this-> p, $ this-> w );
Array_multisort ($ this-> d, SORT_DESC, $ this-> w, $ this-> p );
}

Private function Calculation ($ p, $ w ){
If ($ w = 0) return $ p;

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.