背包问题求解 - java

背包问题的描述:已知有n种物品和一个可容纳M重量的背包,每种物品i的重量为。假定将物品i的一部分放入背包就会得到的效益,这里,, 。显然,由于背包容量是M,因此,要求所有选中要装入背包的物品总重量不得超过M.。如果这n件物品的总重量不超过M,则把所有物品装入背包自然获得最大效益。现需解决的问题是,在这些物品重量的和大于M的情况下,该如何装包,使得得到更大的效益值。

用贪心策略求解背包问题

策略1:按物品价值p降序装包,

策略2:按物品重w升序装包

策略3:按物品价值与重量比值p/w的降序装包

java实现

package com.coffee.test2;

public class Goods {
	private int goodsNum;
	private double[] weights;
	private double[] values;

	// orderbyweight
	public void orderByWeight() {
		// weight temp
		double wtemp = 0.0;
		// value temp;
		double vtemp = 0.0;
		for (int i = 0; i < goodsNum; i++) {
			for (int j = i + 1; i < goodsNum; i++) {
				if (weights[j] > weights[i]) {
					wtemp = weights[i];
					weights[i] = weights[j];
					weights[j] = wtemp;

					vtemp = values[i];
					values[i] = values[j];
					values[j] = vtemp;
				}
			}
		}
	}

	public void orderByValue() {
		double wtemp = 0.0;
		// value temp;
		double vtemp = 0.0;
		for (int i = 0; i < goodsNum; i++) {
			for (int j = i + 1; i < goodsNum; i++) {
				if (values[j] > values[i]) {
					wtemp = weights[i];
					weights[i] = weights[j];
					weights[j] = wtemp;

					vtemp = values[i];
					values[i] = values[j];
					values[j] = vtemp;
				}
			}
		}

	}

	// order by value/weight
	public void orderByWV() {
		double wtemp = 0.0;
		// value temp;
		double vtemp = 0.0;
		double wv[] = new double[goodsNum];
		// init value/weight[]
		for (int i = 0; i < goodsNum; i++) {
			wv[i] = weights[i] / values[i];
		}

		for (int i = 0; i < goodsNum; i++) {
			for (int j = i + 1; i < goodsNum; i++) {
				if (wv[j] > wv[i]) {
					wtemp = weights[i];
					weights[i] = weights[j];
					weights[j] = wtemp;

					vtemp = values[i];
					values[i] = values[j];
					values[j] = vtemp;
				}
			}
		}

	}

	public void print() {
		System.out.print("weight:");
		for (int i = 0; i < goodsNum; i++) {
			System.out.print(weights[i] + "	");
		}
		System.out.println();
		System.out.print("value: ");
		for (int i = 0; i < goodsNum; i++) {
			System.out.print(values[i] + "	");
		}
		System.out.println();
	}
	
	public int getGoodsNum() {
		return goodsNum;
	}
	
	public void setGoodsNum(int goodsNum) {
		this.goodsNum = goodsNum;
	}
	
	public void setWeights(double[] weights) {
		this.weights = weights;
	}
	
	public double[] getWeights() {
		return weights;
	}
	
	public double[] getValues() {
		return values;
	}
	
	public void setValues(double[] values) {
		this.values = values;
	}
}

package com.coffee.test2;
/**
 * define packge class
 * @author apple
 *
 */
public class Packge {
	//max weight
	private double maxweight;
	//have put weight
	private double putweight;
	
	//total values;
	private double putvalues;
	
	
	public double getMax() {
		return maxweight;
	}

	public void setMax(int maxweight) {
		this.maxweight = maxweight;
	}
	
	public double putGoods(Goods goods){
		//last goods,just put part of it
		int last=0;
		for(int i=0;i<goods.getGoodsNum();i++){
			if((putweight+goods.getWeights()[i])>maxweight){
				last=i;
				break;
			}
			putweight+=goods.getWeights()[i];
			putvalues+=goods.getValues()[i];
		}
		//the last good's part weight
		double leisure=maxweight-putweight;
		putvalues+=(goods.getValues()[last]/goods.getWeights()[last])*leisure;
		return putvalues; 
	}
}

package com.coffee.test2;

public class Test {
	public static void main(String[] args) {
		Goods goods = new Goods();
		goods.setGoodsNum(10);
		double[] w = new double[]{ 12, 3 , 5 , 6 , 20 , 18 , 32 , 26 , 30 , 10 };
		double[] v = new double[]{ 20, 16 , 10 , 10 , 4 , 8 , 10 , 2 , 4 , 5 };
		goods.setValues(v);
		goods.setWeights(w);
		goods.print();
		
		Packge p1 = new Packge();
		p1.setMax(100);
	
		//orderbywei
		goods.orderByWeight();
		
		double totalv1=p1.putGoods(goods);

		
		//orderbyvalue
		goods.orderByValue();
		double totalv2=p1.putGoods(goods);
		
		//orderbywei/val
		goods.orderByWV();
		double totalv3=p1.putGoods(goods);
		
		System.out.println("orderbyweight,	        the total v:"+totalv1);
		System.out.println("orderbyvalue, 	        the total v:"+totalv2);
		System.out.println("orderbyweight/values, 	the total v:"+totalv3);
	}
	
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值