17 import java.util.ArrayList;
26 System.loadLibrary(
"jniortools");
29 private static void solve() {
36 final long[] values = {360, 83, 59, 130, 431, 67, 230, 52, 93, 125, 670, 892, 600, 38, 48, 147,
37 78, 256, 63, 17, 120, 164, 432, 35, 92, 110, 22, 42, 50, 323, 514, 28, 87, 73, 78, 15, 26,
38 78, 210, 36, 85, 189, 274, 43, 33, 10, 19, 389, 276, 312};
40 final long[][] weights = {{7, 0, 30, 22, 80, 94, 11, 81, 70, 64, 59, 18, 0, 36, 3, 8, 15, 42, 9,
41 0, 42, 47, 52, 32, 26, 48, 55, 6, 29, 84, 2, 4, 18, 56, 7, 29, 93, 44, 71, 3, 86, 66, 31,
42 65, 0, 79, 20, 65, 52, 13}};
44 final long[] capacities = {850};
48 solver.
init(values, weights, capacities);
49 final long computedValue = solver.
solve();
53 ArrayList<Integer> packedItems =
new ArrayList<Integer>();
54 ArrayList<Long> packedWeights =
new ArrayList<Long>();
56 System.out.println(
"Total value = " + computedValue);
57 for (
int i = 0; i < values.length; i++) {
60 packedWeights.add(weights[0][i]);
61 totalWeight = (int) (totalWeight + weights[0][i]);
64 System.out.println(
"Total weight: " + totalWeight);
65 System.out.println(
"Packed items: " + packedItems);
66 System.out.println(
"Packed weights: " + packedWeights);
70 public static void main(String[] args)
throws Exception {
static void main(String[] args)
Sample showing how to model using the knapsack solver.