import java.util.Arrays;
import java.util.Random;
class LogisticsOptimizer {
public static void main
(String[] args
) { // Randomly generated problem data
String[] factories
= {"Seattle",
"Portland",
"Denver"}; String[] stores
= {"Los_Angeles",
"Phoenix",
"Dallas",
"Chicago"};
int[] production = {
rand.nextInt(100) + 50, // 50-150 units
rand.nextInt(120) + 60, // 60-180 units
rand.nextInt(80) + 70 // 70-150 units
};
int[] requirements = {
rand.nextInt(90) + 40, // 40-130 units
rand.nextInt(70) + 50, // 50-120 units
rand.nextInt(110) + 30, // 30-140 units
rand.nextInt(60) + 60 // 60-120 units
};
// Balance demand if needed
int totalSupply
= Arrays.
stream(production
).
sum(); int totalDemand
= Arrays.
stream(requirements
).
sum(); if (totalSupply != totalDemand) {
requirements[0] += totalSupply - totalDemand;
}
// Randomized cost matrix (per unit)
int[][] shippingCosts = new int[factories.length][stores.length];
for (int i = 0; i < factories.length; i++) {
for (int j = 0; j < stores.length; j++) {
shippingCosts[i][j] = rand.nextInt(500) + 200; // $200-$700
}
}
// Solve using Northwest Corner Method
int[][] allocations = solveNorthwestCorner(production, requirements);
// Display results
printSolution(factories, stores, production, requirements,
shippingCosts, allocations);
}
private static int[][] solveNorthwestCorner(int[] supply, int[] demand) {
int[][] solution = new int[supply.length][demand.length];
int[] remainingSupply
= Arrays.
copyOf(supply, supply.
length); int[] remainingDemand
= Arrays.
copyOf(demand, demand.
length);
int i = 0, j = 0;
while (i < supply.length && j < demand.length) {
int amount
= Math.
min(remainingSupply
[i
], remainingDemand
[j
]); solution[i][j] = amount;
remainingSupply[i] -= amount;
remainingDemand[j] -= amount;
if (remainingSupply[i] == 0) i++;
if (remainingDemand[j] == 0) j++;
}
return solution;
}
private static void printSolution
(String[] sources,
String[] destinations,
int[] supply, int[] demand,
int[][] costs, int[][] solution) {
System.
out.
println("=== Logistics Optimization Report ==="); System.
out.
println("\nSources:"); for (int i = 0; i < sources.length; i++) {
System.
out.
printf("- %-10s: %d units%n", sources
[i
], supply
[i
]); }
System.
out.
println("\nDestinations:"); for (int j = 0; j < destinations.length; j++) {
System.
out.
printf("- %-10s: %d units%n", destinations
[j
], demand
[j
]); }
System.
out.
println("\nOptimal Allocation Plan:"); int totalCost = 0;
for (int i = 0; i < solution.length; i++) {
for (int j = 0; j < solution[0].length; j++) {
if (solution[i][j] > 0) {
int cost = solution[i][j] * costs[i][j];
System.
out.
printf("%-10s → %-10s: %4d units x $%3d = $%6d%n",
sources[i], destinations[j],
solution[i][j], costs[i][j], cost);
totalCost += cost;
}
}
}
System.
out.
printf("%nTotal Transportation Cost: $%,d%n", totalCost
); }
}
aW1wb3J0IGphdmEudXRpbC5BcnJheXM7CmltcG9ydCBqYXZhLnV0aWwuUmFuZG9tOwoKY2xhc3MgTG9naXN0aWNzT3B0aW1pemVyIHsKCiAgICBwdWJsaWMgc3RhdGljIHZvaWQgbWFpbihTdHJpbmdbXSBhcmdzKSB7CiAgICAgICAgLy8gUmFuZG9tbHkgZ2VuZXJhdGVkIHByb2JsZW0gZGF0YQogICAgICAgIFN0cmluZ1tdIGZhY3RvcmllcyA9IHsiU2VhdHRsZSIsICJQb3J0bGFuZCIsICJEZW52ZXIifTsKICAgICAgICBTdHJpbmdbXSBzdG9yZXMgPSB7Ikxvc19BbmdlbGVzIiwgIlBob2VuaXgiLCAiRGFsbGFzIiwgIkNoaWNhZ28ifTsKICAgICAgICAKICAgICAgICBSYW5kb20gcmFuZCA9IG5ldyBSYW5kb20oKTsKICAgICAgICBpbnRbXSBwcm9kdWN0aW9uID0gewogICAgICAgICAgICByYW5kLm5leHRJbnQoMTAwKSArIDUwLCAgLy8gNTAtMTUwIHVuaXRzCiAgICAgICAgICAgIHJhbmQubmV4dEludCgxMjApICsgNjAsICAvLyA2MC0xODAgdW5pdHMKICAgICAgICAgICAgcmFuZC5uZXh0SW50KDgwKSArIDcwICAgIC8vIDcwLTE1MCB1bml0cwogICAgICAgIH07CiAgICAgICAgCiAgICAgICAgaW50W10gcmVxdWlyZW1lbnRzID0gewogICAgICAgICAgICBy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lc3QgQ29ybmVyIE1ldGhvZAogICAgICAgIGludFtdW10gYWxsb2NhdGlvbnMgPSBzb2x2ZU5vcnRod2VzdENvcm5lcihwcm9kdWN0aW9uLCByZXF1aXJlbWVudHMpOwogICAgICAgIAogICAgICAgIC8vIERpc3BsYXkgcmVzdWx0cwogICAgICAgIHByaW50U29sdXRpb24oZmFjdG9yaWVzLCBzdG9yZXMsIHByb2R1Y3Rpb24sIHJlcXVpcmVtZW50cywgCiAgICAgICAgICAgICAgICAgICAgIHNoaXBwaW5nQ29zdHMsIGFsbG9jYXRpb25zKTsKICAgIH0KCiAgICBwcml2YXRlIHN0YXRpYyBpbnRbXVtdIHNvbHZlTm9ydGh3ZXN0Q29ybmVyKGludFtdIHN1cHBseSwgaW50W10gZGVtYW5kKSB7CiAgICAgICAgaW50W11bXSBzb2x1dGlvbiA9IG5ldyBpbnRbc3VwcGx5Lmxlbmd0aF1bZGVtYW5kLmxlbmd0aF07CiAgICAgICAgaW50W10gcmVtYWluaW5nU3VwcGx5ID0gQXJyYXlzLmNvcHlPZihzdXBwbHksIHN1cHBseS5sZW5ndGgpOwogICAgICAgIGludFtdIHJlbWFpbmluZ0RlbWFuZCA9IEFycmF5cy5jb3B5T2YoZGVtYW5kLCBkZW1hbmQubGVuZ3RoKTsKICAgICAgICAKICAgICAgICBpbnQgaSA9IDAsIGogPSAwOwogICAgICAgIHdoaWxlIChpIDwgc3VwcGx5Lmxlbmd0aCAmJiBqIDwgZGVtYW5kLmxlbmd0aCkgewogICAgICAgICAgICBpbnQgYW1vdW50ID0gTWF0aC5taW4ocmVtYWluaW5nU3VwcGx5W2ldLCByZW1haW5pbmdEZW1hbmRbal0pOwogICAgICAgICAgICBzb2x1dGlvbltpXVtqXSA9IGFtb3VudDsKICAgICAgICAgICAgcmVtYWluaW5nU3VwcGx5W2ldIC09IGFtb3VudDsKICAgICAgICAgICAgcmVtYWluaW5nRGVtYW5kW2pdIC09IGFtb3VudDsKICAgICAgICAgICAgCiAgICAgICAgICAgIGlmIChyZW1haW5pbmdTdXBwbHlbaV0gPT0gMCkgaSsrOwogICAgICAgICAgICBpZiAocmVtYWluaW5nRGVtYW5kW2pdID09IDApIGorKzsKICAgICAgICB9CiAgICAgICAgcmV0dXJuIHNvbHV0aW9uOwogICAgfQoKICAgIHByaXZhdGUgc3RhdGljIHZvaWQgcHJpbnRTb2x1dGlvbihTdHJpbmdbXSBzb3VyY2VzLCBTdHJpbmdbXSBkZXN0aW5hdGlvbnMsCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGludFtdIHN1cHBseSwgaW50W10gZGVtYW5kLAogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBpbnRbXVtdIGNvc3RzLCBpbnRbXVtdIHNvbHV0aW9uKSB7CiAgICAgICAgU3lzdGVtLm91dC5wcmludGxuKCI9PT0gTG9naXN0aWNzIE9wdGltaXphdGlvbiBSZXBvcnQgPT09Iik7CiAgICAgICAgU3lzdGVtLm91dC5wcmludGxuKCJcblNvdXJjZXM6Iik7CiAgICAgICAgZm9yIChpbnQgaSA9IDA7IGkgPCBzb3VyY2VzLmxlbmd0aDsgaSsrKSB7CiAgICAgICAgICAgIFN5c3RlbS5vdXQucHJpbnRmKCItICUtMTBzOiAlZCB1bml0cyVuIiwgc291cmNlc1tpXSwgc3VwcGx5W2ldKTsKICAgICAgICB9CiAgICAgICAgCiAgICAgICAgU3lzdGVtLm91dC5wcmludGxuKCJcbkRlc3RpbmF0aW9uczoiKTsKICAgICAgICBmb3IgKGludCBqID0gMDsgaiA8IGRlc3RpbmF0aW9ucy5sZW5ndGg7IGorKykgewogICAgICAgICAgICBTeXN0ZW0ub3V0LnByaW50ZigiLSAlLTEwczogJWQgdW5pdHMlbiIsIGRlc3RpbmF0aW9uc1tqXSwgZGVtYW5kW2pdKTsKICAgICAgICB9CiAgICAgICAgCiAgICAgICAgU3lzdGVtLm91dC5wcmludGxuKCJcbk9wdGltYWwgQWxsb2NhdGlvbiBQbGFuOiIpOwogICAgICAgIGludCB0b3RhbENvc3QgPSAwOwogICAgICAgIGZvciAoaW50IGkgPSAwOyBpIDwgc29sdXRpb24ubGVuZ3RoOyBpKyspIHsKICAgICAgICAgICAgZm9yIChpbnQgaiA9IDA7IGogPCBzb2x1dGlvblswXS5sZW5ndGg7IGorKykgewogICAgICAgICAgICAgICAgaWYgKHNvbHV0aW9uW2ldW2pdID4gMCkgewogICAgICAgICAgICAgICAgICAgIGludCBjb3N0ID0gc29sdXRpb25baV1bal0gKiBjb3N0c1tpXVtqXTsKICAgICAgICAgICAgICAgICAgICBTeXN0ZW0ub3V0LnByaW50ZigiJS0xMHMg4oaSICUtMTBzOiAlNGQgdW5pdHMgeCAkJTNkID0gJCU2ZCVuIiwKICAgICAgICAgICAgICAgICAgICAgICAgICAgIHNvdXJjZXNbaV0sIGRlc3RpbmF0aW9uc1tqXSwKICAgICAgICAgICAgICAgICAgICAgICAgICAgIHNvbHV0aW9uW2ldW2pdLCBjb3N0c1tpXVtqXSwgY29zdCk7CiAgICAgICAgICAgICAgICAgICAgdG90YWxDb3N0ICs9IGNvc3Q7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgU3lzdGVtLm91dC5wcmludGYoIiVuVG90YWwgVHJhbnNwb3J0YXRpb24gQ29zdDogJCUsZCVuIiwgdG90YWxDb3N0KTsKICAgIH0KfQ==