重量最小者优先:在剩余物品中,选择可以装入背包的重量最小的物品。
但这种策略,不能保证重量小的是最有价值的,也不能得到最优解。例如:
AA , 得 到 的 解 为 . 而 最 优 解 是
。
单位 价 值最 大者 优先 : 根据 价值与重量的比值,即单位价值,在
剩下的物品中依次选取比值最大的 物品装入背包。这种策略也不能得到
最优解。例如:@AA>AAA@A@@得
到的解 .而最优解是。但它是直觉上的一个近似解。本文讨论
该策略。
策略 @ 的具体步骤为:
第 一步 :计 算每 个 物品 的价 值 比,。
第 二步 :对 物品 的 价值 比 非递 增排序。
第三步:重复下面操作,直到有序列表中留下物品。如果列表中的当前物
品能够装入背包,就将它放入背包中,否则,处理下一个物品。
2.2.2 编程实现
: 5.-$:
% !"
!-$"%
-$"
#% !& '
(%! .%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%自定义物品最大数
%& 8 #%%%%%%%%%%定义包函数
*
%%%%%%%, ! .'
%%%%%%% .! .'
%%%%%%%5''77
%%%%%%%*
%%%%%%%%%%%%%% ,'%%%%%%%%%%单位价值计算
%%%%%%%%%%%%%%.'
%%%%%%%+
%%%%%%%5''77
%%%%%%%*
%%%%%%%%%%%%%%5%C'C'C77
%%%%%%%%%%%%%%*
%%%%%%%%%%%%%%%%%%%%%5 C C7%%%%%%%%%进行循环判断
%%%%%%%%%%%%%%%%%%%%%*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%,%, C'
%%%%%%%%%%%%%%%%%%%%%%%%%%%% C C7'
%%%%%%%%%%%%%%%%%%%%%%%%%%%% C7,'
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%C'
%%%%%%%%%%%%%%%%%%%%%%%%%%%%CC7'
%%%%%%%%%%%%%%%%%%%%%%%%%%%%C7'