0-1背包问题的java实现(回溯法)
具体问题描述以及C/C++实现参见网址
http://blog.csdn.net/liufeng_king/article/details/8764319
/**
* 0-1背包问题--回溯法
* @author Lican
*
*/
public class Knapsack {
public static class Element implements Comparable{
int id;//物品编号
double d;
public Element(int id,double d){
this.id=id;
this.d=d;
}
@Override
public int compareTo(Object x) {
double xd=((Element)x).d;//递减顺序排列
if(d
if(d==xd) return 0;
return 1;
}
}
double c;//背包容量
int n;//物品数
double[] w;//物品重量数组
double[] p;//物品价值数组
double cw;//当前重量
double cp;//当前价值
double bestp;//最优价值
int[] x;//当前装入背包顺序
int[] bestx;//最优装入背包顺序
Element[] q;//q为单位重量价值数组</