public class Knapsack {
private final double[] p, w; 价值,重量
private final int n;
private final double c;
private double bestp, cp, cw;
private final int x[]; 记录可选的物品
private final int cx[];
public Knapsack(final double pp[], final double ww[], final double cc) {
this.p = pp;
this.w = ww;
this.n = pp.length - 1;
this.c = cc;
this.cp = 0;
this.cw = 0;
this.bestp = 0;
x = new int[ww.length];
cx = new int[pp.length];
}
void knapsack() {
backtrack(0);
}
void backtrack(final int i) {
if (i > n) { 判断是否到达了叶子节点
if (cp > bestp) {
bestp = cp;
for (int j = 0; j < x.length; j++)
x[j] = cx[j];
本内容试读结束,登录后可阅读更多
下载后可阅读完整内容,剩余2页未读,立即下载