1. Questions
Known existing items m pieces (item 0,1,2,3,... m-1), the maximum load bearing of the backpack is N, calculate the maximum value that can be achieved by putting this m-item into this backpack.
The name, weight, and value of each item are stored.
/**
* denotes the class of each item.
*
/class bagitem{
String name;//Item name
int weight;//item weight
int value;//Item Value public
Bagitem ( String Name,int weight,int value) {
this.name = name;
This.weight = weight;
This.value = value;
}
}
2. Use the idea of dynamic planning to do this topic
The physical meaning of f[i][j] is the maximum weight that can be obtained in a backpack with a maximum load-bearing of J.
Decision-making method: Whether the item I is put in depends on the value F[i-1][j-b[i].weight]+b[i].value (j>=b[i].weight) and the value that is not put in the article I item f[i-1][j] compare the results.
S[I][J] Indicates the name of each item in the backpack when f[i][j] gets the maximum value.
The f[m-1][n in this final Java array] is the maximum weight of the m item that can be placed in a backpack with a maximum load of n. S[m-1][n] is the final result.
/** * Solve 0-1 knapsack problem * Dynamic planning.
* In physical sense f[i][j] the maximum weight available in a backpack with a maximum load-bearing of j is stored in the first I item.
* Whether the item I is put in depends on the value of F[i-1][j-b[i].weight]+b[i].value (j>=b[i].weight) and the value of the item I do not put in article I f[i-1][j] comparison results.
* S[i][j] Indicates the name of each item in the backpack when f[i][j] gets the maximum value.
* */public class BAG01 {public int[][] f;
Public string[][] s;
int m;//total number of items int n;//Backpack Maximum load-bearing public Bag01 (int m,int n) {this.m = m;
THIS.N = n;
f = new Int[m][n];
s = new String[m][n];
for (int i=0;i<m;i++) {for (int j=0;j<n;j++) {S[i][j] = ""; }}}} public void Selectbigvalue (bagitem[] B) {/* initialization code NO. 0 Item Operation operation */for (int j=0;j<n;j++) {if (J>=b[0].wei
ght) {F[0][j] = B[0].value;
S[0][J] = B[0].name; }}/* Follow the decision formula to determine if the next item is to be placed in the backpack */for (int i=1;i<m;i++) {for (int j=0;j<n;j++) {if (J>=b[i].weight &&am P
F[i-1][j-b[i].weight]+b[i].value > F[i-1][j]) {f[i][j] = F[i-1][j-b[i].weight] + b[i].value;
S[I][J] = S[i-1][j-b[i].weight] + b[i].name; }else{F[i][j] = f[i-1][J];
S[I][J] = S[i-1][j]; }
}
}
}
}
You need to be aware of when calling:
If the maximum load of the backpack is 10 o'clock, the initialization matrix is 11 lines, J can be taken to 10.
public static void Main (string[] args) {
bagitem[] B = new Bagitem[5];
B[0] = new Bagitem ("F", 2,6);
B[1] = new Bagitem ("C", 4,6);
B[2] = new Bagitem ("D", 5,4);
B[3] = new Bagitem ("E", 6,5);
B[4] = new Bagitem ("B", 2,3);
*/* Note: When the backpack's maximum load is 10 o'clock, the initialization matrix is 11 lines, J can be taken to 10*/
Bag01 test = new BAG01 (5,10);
Test.selectbigvalue (B);
/* value matrix in output backpack */for
(int i=0;i<test.m;i++) {
System.out.print (int j=0;j<test.n;j++) { test.f[i][j]+ " ");
}
System.out.println ();
}
/* output corresponds to F[I][J] The item name in the backpack when the maximum value is obtained. */For
(int i=0;i<test.m;i++) {for
(int j=0;j<test.n;j++) {
System.out.print (test.s[i][j]+ " ");
}
System.out.println ();
}
}
The execution results of the code are as follows