简体   繁体   English

0/1 背包问题 - 需要浮点/双值的解决方案

[英]0/1 Knapsack problem - Need solution for float/double values

Looking for solution/code for 0/1 Knapsack problem where the input weights and values are float/double instead of int.寻找 0/1 背包问题的解决方案/代码,其中输入权重和值是 float/double 而不是 int。
Eg:例如:
double val[] = { 8.2, 6.8, 6.5, 6.2, 5.9, 5.5, 5.4, 5.2, 5.1, 5 };双 val[] = { 8.2, 6.8, 6.5, 6.2, 5.9, 5.5, 5.4, 5.2, 5.1, 5 };
double wt[] = { 13, 7.3, 6.7, 10.7, 7, 8.5, 12.1, 8, 10.7, 7.5 };双 wt[] = { 13, 7.3, 6.7, 10.7, 7, 8.5, 12.1, 8, 10.7, 7.5 };

First of all, if only values (but not weights) are doubles then it should be no problem (assuming you are using the typical 2D table method).首先,如果只有值(而不是权重)是双精度值,那么应该没有问题(假设您使用的是典型的二维表方法)。 If we have weights as doubles then one workaround is to treat these doubles as integers, by multiplying all weights by 10 to the power of (the min number of digits needed to make all weights integers), which is 1 in your example because all numbers use at most one digit for precision.如果我们将权重作为双精度数,那么一种解决方法是将这些双精度数视为整数,方法是将所有权重乘以 10 的次方(使所有权重成为整数所需的最小位数),在您的示例中为 1,因为所有数字最多使用一位数字来保证精度。 The overall time complexity of this solution becomes O(N.C.10^(D)) where N is the number of items, C is the max possible capacity, and D is the max number of digits needed as precision.该解决方案的总体时间复杂度变为 O(N.C.10^(D)),其中 N 是项目数,C 是最大可能容量,D 是精度所需的最大位数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM