gpt4 book ai didi

java - 选择 0/1 背包中的元素,其中两个元素具有相同的 yield |最大化值(value)和最小化重量

转载 作者:搜寻专家 更新时间:2023-11-01 03:49:53 25 4
gpt4 key购买 nike

在 0/1 背包问题中,如果两个项目具有相同的值,我如何选择项目。应该选择权重较小的值,如何检查该条件?我有以下使用动态编程的功能。

static int[] knapsack(int maxWeight, double[] weight, double[] value, int n) {
//n = no. if items
int i, w;

double array[][] = new double[n + 1][maxWeight + 1];
for (i = 0; i <= n; i++) {
for (w = 0; w <= maxWeight; w++) {
if (i == 0 || w == 0)
array[i][w] = 0;
else if (weight[i - 1] <= w)
array[i][w] = max(value[i - 1] + array[i - 1][(w -(int) weight[i - 1])], array[i - 1][w]);
else
array[i][w] = array[i - 1][w];
if (i != 0 || w != 0)
System.out.print(array[i][w] + "\t");
}
System.out.println();
}

int[] selected = new int[n + 1];
for (int j = n, wt = maxWeight; j > 0; j--) {
if (array[j][wt] != array[j - 1][wt]) {
if (array[j][wt] == array[j][wt - 1]) {
selected[j] = 0;
break;
}
selected[j] = 1;
wt = wt - (int) weight[j - 1];
}
else
selected[j] = 0;
}
/** Print finally selected items **/
System.out.println("\nItems selected : ");
for (int k = 1; k < n + 1; k++)
if (selected[k] == 1)
System.out.print(k +" ");
System.out.println();

return selected;
}

对于这种情况:(i,v): (4,45)(3,20)(5,30)(2,45) ,maxWeight = 5; 如果第 1 项和第 4 项具有相同的值,则应选择权重较小的第 4 项。我如何在上面的代码中实现这个条件。问题陈述:

Your goal is to determine which things to put into the package so that the total weight is less than or equal to the package limit and the total cost is as large as possible. You would prefer to send a package which weights less in case there is more than one package with the same price.

最佳答案

如果您的意思是通过最小化权重最大化值(value)。你可以检查一下

令DP[i][j]为可获得的最大值!

W[i][j] 是要使用的最小权重!!

然后,

if(Current Weight > Element's Weight)
{
if(DP[i-1][j-Weight[i]]+Value[i]>DP[i-1][j]){
DP[i][j]=DP[i-1][j-Weight[i]]+Value[i];
Weight[i][j]= Weight[i-1][j-Weight[i]]+Value[i]
}
else if(DP[i-1][j-Weight[i]]+Value[i] < DP[i-1][j] ){
DP[i][j]=DP[i-1][j];
Weight[i][j]=Weight[i-1][j];
}
else{ //Note this is the tricky part elsewise the
//Above conditions are simple Knapsack conditions

DP[i][j]=DP[i-1][j]; //Both of them are equal We will get same Value . Thus we cannot maximise it in any other way!!
Weight[i][j]=minimum ( Weight[i-1][j] ,Weight[i-1][j-Weight[i]]+A[i]);

}
}
else
{
DP[i][j]=DP[i-1][j];
Weight[i][j]=Weight[i-1][j];
}

注意解决方案很简单,除非第一个 if 中的第三个条件!我们需要不惜一切代价最大化乐趣!所以我们不是在搞乱它!但是当两种情况的乐趣相同时,我们需要选择重量更轻的那个,否则我们最终会以相同的背包重量获得更多的重量!

我假设你知道背包 0/1 问题,这就是为什么我没有解释第一个和第二个条件!!

关于java - 选择 0/1 背包中的元素,其中两个元素具有相同的 yield |最大化值(value)和最小化重量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31482546/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com