Knapsack problem - branch and bound

 

 

import java.util.*;
public class Main{
public static final int ASD = 1000000007;

public static void main(String[] args){

Scanner sc = new Scanner(System.in);
int k=sc.nextInt();
int a=sc.nextInt();
int x=sc.nextInt();
int b=sc.nextInt();
int y=sc.nextInt();
int sum = 0;
//k=17;a=5;x=79;b=4;y=66;
//k=205;a=1;x=92;b=4;y=92;
//3615040
sc.close();

int[] n = new int[x + y];
int[] e = new int[x + y];
for(int i = 0; i < n.length; i++){
if(i < x){
e[i] = a;
}
The else {
E [I] = B;
}
n-[I] = 0;
}

SUM = DP (n-, E, K, 0, -1);
System.out.println (SUM);
}

public static int DP ( int [] n, int [] e, int k, int sum, int) {yn

branch after time if (meetRequire (n, e, k) == 2) {// no more than k determination.
SUM return;
}

Yn = Yn +. 1;

n-[Yn] =. 1;
IF (Yn <-n.length. 1) {
SUM = DP (n-, E, K, SUM, Yn);
}

IF (meetRequire (n-, E, K) ==. 1) {
SUM = SUM +. 1;
}
System.out.println("" + n[0] + n[1] + n[2] + n[3] + n[4] + n[5] + " " + sum);

n[yn] = 0;
if(yn < n.length-1){
sum = dp(n, e, k, sum, yn);
}


return sum;
}


public static int meetRequire(int[] n, int[] e, int k){
int s = 0;
for(int i = 0; i < n.length; i++){
if(n[i] == 1){
s = s + e[i];
}
}
if(s < k){
//System.out.println("" + n[0] + n[1] + n[2] + n[3] + n[4] + n[5] );
return 0;
}
else if(s == k){
//System.out.println("" + n[0] + n[1] + n[2] + n[3] + n[4] + n[5] );
return 1;
}
else{
return 2;
}
}
}

Recursive binary tree traversal - - |||

= Branch and bound method. = ||

30% case through rate, the more stupid. . .

 

reference

https://www.bilibili.com/video/av50202822?from=search&seid=13676114442420163856

 

Guess you like

Origin www.cnblogs.com/clamp7724/p/11387671.html