0-1beibao

#include <cstdio>
#include <algorithm>
the using namespace STD;
struct TR {
int weight;
int value;
};
int casenum;
int ObjectNum;
int bagw; // limit the weight of the backpack
TR Tab [200 is];
int = MAXV - . 1;
int currentw;
int currentv;
BOOL Compare (TR A, TR B) {
IF (((Double) a.value / a.weight)> ((Double) b.value / b.weight)) return to true;
return to false;
}
Double tanxin_shangjie (int i) {// this article starts from the greedy loaded i, the upper bound is obtained

the_rest_of_w = Double (Double) bagw-currentw;
Double value_after_tanxin = (Double) currentv;
// the printf ( "not performed before the greedy, the rest of the backpack weight:% f \ n", the_rest_of_w );

// printf ( "before not be greedy, the current value:% f \ n", value_after_tanxin);
// printf ( "% d start selecting the first piece: the optimal choice of greedy border on select \ n" , i);

while(i<=objectnum&&tab[i].weight<=the_rest_of_w){

// printf ( "come yet \ n-");
// printf ( "first item of value D% \ n-", Tab [. 1] .Value);
// printf ( "% value of the first items f \ n ", (double) tab [1] .value);

value_after_tanxin+=(double)tab[i].value;

 

// printf ( "up to the maximum upper bound value: F% \ n-", value_after_tanxin);
the_rest_of_w = the_rest_of_w-Tab [I] .Weight;
I ++;
}
IF (I <= ObjectNum) {
// printf ( "incoming cleavage supplementary backpack \ the n-");
// when the backpack, i would not hold all the items, because, hold all the items, then" n the
// because without all the equipment, so this time i went down on behalf of but without a means to hold the article
value_after_tanxin value_after_tanxin + Tab = [I] * .Value (the_rest_of_w / Tab [I] .Weight);
}
// the printf ( "% d to the second end member, that is, items of% d is divided into: the upper bound of the maximum value of% f \ n ", i, i, value_after_tanxin); // all items are Bahrain, where + 1 exceeds the number of items will appear member
return value_after_tanxin;
}

void dfs (int i) {// the i items, or not to do
if (i> objectnum) {// iterate over all the items of the
IF (currentv> MAXV) {
MAXV = currentv;
}
}
the else {
// Including this one i items, which can not match the current maximum upper bound of the optimal value, here are whether you have or not to have the i no,
// but this is not good, because so long as the items to be chosen , then this upper bound will be re-considered, duplication of work done, that is, only if the item is not currently selected, the upper bound appears change
// but it is taken into account, when you first enter the selection, in fact, Ye Haha not bound by calculation
// printf ( "% d of items, and not into the selected state selected from the group \ n", i);

if (currentw + tab [i] .weight <= bagw) {// this Item I to the
currentw currentw + Tab = [I] .Weight;
currentv currentv + Tab = [I] .Value;
// the printf ( "this No.% d is I want ------------------------ value is% d \ n ", i, tab [i] .value);
DFS (i +. 1);
currentw = currentw-Tab [i] .Weight;
currentv = currentv-Tab [i] .Value;
}
IF (tanxin_shangjie (i)> MAXV) {
// case where the i-th item has been consider, then the i-th article do not, then this will certainly have to reconsider, because you put the most valuable pieces of the i are not, in front of the IF
// to the case of the first i items under it made, in case you do not first i items of this tanxin directly to the judge in the case to the i + 1 case of goods, the maximum, or if you can, then enter if it becomes, cumbersome calculation the
// so if I change, if no longer following this judgment, because the judge has already passed
// printf ( "% d of this one, not the --------------- --------------- value of D% \ n-", I, Tab [I] .Value);
DFS (I +. 1);

}
}


}
Int main () {
Scanf ( "% D", & casenum);
for (int K =. 1; K <= casenum; ++ K) {
Scanf ( "% D% D", & ObjectNum, & bagw);
// the printf ( "% D% D", ObjectNum, bagw);
for (int I =. 1; I <= ObjectNum; ++ I) {
Scanf ( "% D% D", & Tab [I] .Weight, & Tab [I] .Value);
}
Sort (+ Tab. 1, Tab. 1 + + ObjectNum, Compare); // the front row valuable items, for depth-first, so that optimization would be better, faster prune
/ * for (int . 1 = I; I <= ObjectNum; I ++) {
the printf ( "% D% D \ n-", Tab [I] .Weight, Tab [I] .Value);
} * /
MAXV = -1;
currentw = 0;
currentv = 0; // remember to change it into 0;
DFS (. 1);
the printf ( "% D \ n-", MAXV);
}
}

Guess you like

Origin www.cnblogs.com/waibizi/p/12070202.html