分枝定界背包 import java.io.*; 导入java.util.*; 类节点{ 智力水平; 国际利润; 整数权重; 内界; } 公共类背包{ 公共静态void main(字符串args[])引发异常{ int最大利润; int N; int W; int-wt; int vl; int-maxVal; 扫描仪输入=新扫描仪(System.in); System.out.println(“请输入项目数:”); N=input.nextInt(); System.out.println(“请输入背包的容量:”); W=输入。nextInt(); int[]Vl=新的int[N]; int[]Wt=新的int[N]; 对于(int i=0;imaxProfit){ Q.添加(u); } } 回报最大利润; } } 我的程序遇到了一个问题,我的输出错误。我已经从C++中转换成了我在网上找到的另一个程序,它工作正常。

分枝定界背包 import java.io.*; 导入java.util.*; 类节点{ 智力水平; 国际利润; 整数权重; 内界; } 公共类背包{ 公共静态void main(字符串args[])引发异常{ int最大利润; int N; int W; int-wt; int vl; int-maxVal; 扫描仪输入=新扫描仪(System.in); System.out.println(“请输入项目数:”); N=input.nextInt(); System.out.println(“请输入背包的容量:”); W=输入。nextInt(); int[]Vl=新的int[N]; int[]Wt=新的int[N]; 对于(int i=0;imaxProfit){ Q.添加(u); } } 回报最大利润; } } 我的程序遇到了一个问题,我的输出错误。我已经从C++中转换成了我在网上找到的另一个程序,它工作正常。,java,knapsack-problem,branch-and-bound,Java,Knapsack Problem,Branch And Bound,输入: 四, 六, 210 312 4.5 3 15 输出应为: 二十二 获得的产出: 12你的复制技能还有很多需要改进的地方——如果你添加了一个指向原始来源的链接,这会很有帮助 看来你已经添加了这些行 import java.io.*; import java.util.*; class node{ int level; int profit; int weight; int bound; } public class KnapsackBB{

输入:

四,

六,

210

312

4.5

3 15

输出应为:

二十二

获得的产出:


12

你的复制技能还有很多需要改进的地方——如果你添加了一个指向原始来源的链接,这会很有帮助

看来你已经添加了这些行

import java.io.*;
import java.util.*;

class node{
    int level;
    int profit;
    int weight;
    int bound;
}

public class KnapsackBB{
        public static void main(String args[])throws Exception{
                int maxProfit;
                int N;
                int W;
                int wt;
                int vl;
                int maxVal;

                Scanner input = new Scanner(System.in);

                System.out.println("Please enter the number of items: ");
                N = input.nextInt();
                System.out.println("Please enter the capacity of the Knapsack: ");
                W = input.nextInt();

                int[] Vl = new int[N];
                int[] Wt = new int[N];

                for(int i = 0; i < N; i++){
                        System.out.println("Please enter the weight anc value of item " + i + ": ");
                        wt = input.nextInt();
                        vl = input.nextInt();

                        Wt[i] = wt;
                        Vl[i] = vl;
                }


                //for(int i  = 0; i < 1000; i++){
                        maxVal = knapsack(N, Vl, Wt, W);
                //}

                System.out.println(maxVal);

        }


        public static int bound(node u, int n, int W, int[] pVa, int[] wVa){
                int j = 0, k = 0;
                int totweight = 0;
                int result = 0;

                if (u.weight >= W){
                        return 0;
                }
                else{
                        result = u.profit;
                        j = u.level + 1;
                        totweight = u.weight;

                        while ((j < n) && (totweight + wVa[j] <= W)){
                                totweight = totweight + wVa[j];
                                result = result + pVa[j];
                                j++;
                        }

                        k = j;

                        if (k < n){
                                result = result + (W - totweight) * pVa[k]/wVa[k];
                        }
                        return result;
                }
        }

        public static int knapsack(int n, int[] p, int[] w, int W){
                Queue<node> Q = new LinkedList<node>();
                node u = new node();
                node v = new node();
                int[] pV = new int[p.length];
                int[] wV = new int[w.length];
                Q.poll();

                for (int i = 0; i < n; i++){
                        pV[i] = p[i];
                        wV[i] = w[i];
                }

                v.level = -1;
                v.profit = 0;
                v.weight = 0;

                int maxProfit = 0;

                //v.bound = bound(v, n, W, pV, wV);
                Q.add(v);

                while (Q.size() > 0){
                        v = Q.remove();

                        if (v.level == -1){
                                u.level = 0;
                        }
                        else if (v.level != (n - 1)){
                                u.level = v.level + 1;
                        }

                        u.weight = v.weight + w[u.level];
                        u.profit = v.profit + p[u.level];

                        u.bound = bound(u, n, W, pV, wV);

                        if (u.weight <= W && u.profit > maxProfit){
                                maxProfit = u.profit;
                        }

                        if (u.bound > maxProfit){
                                Q.add(u);
                        }

                        u.weight = v.weight;
                        u.profit = v.profit;

                        u.bound = bound(u, n, W, pV, wV);

                        if (u.bound > maxProfit){
                                Q.add(u);
                        }

                }
                return maxProfit;
        }
}
两次。至少这提供了你想要的“22”答案

也就是说,该解决方案远不是面向对象的,而是一般的Java风格

u.weight = v.weight;
u.profit = v.profit;
u.bound = bound(u, n, W, pV, wV);
if (u.bound > maxProfit){
    Q.add(u);
}