14 package com.google.ortools.graph;
16 import static org.junit.jupiter.api.Assertions.assertEquals;
17 import static org.junit.jupiter.api.Assertions.assertNotNull;
19 import com.google.ortools.Loader;
20 import org.junit.jupiter.api.BeforeEach;
21 import org.junit.jupiter.api.Test;
32 final int numSources = 4;
33 final int numTargets = 4;
34 final int[][] costs = {
35 {90, 75, 75, 80}, {35, 85, 55, 65}, {125, 95, 90, 105}, {45, 110, 95, 115}};
36 final int expectedCost = 275;
37 final MinCostFlow minCostFlow =
new MinCostFlow();
38 assertNotNull(minCostFlow);
39 for (
int source = 0; source < numSources; ++source) {
40 for (
int target = 0; target < numTargets; ++target) {
41 minCostFlow.addArcWithCapacityAndUnitCost(
42 source, numSources + target, 1, costs[source][target]);
46 for (
int source = 0; source < numSources; ++source) {
47 minCostFlow.setNodeSupply(source, 1);
49 for (
int target = 0; target < numTargets; ++target) {
50 minCostFlow.setNodeSupply(numSources + target, -1);
52 final MinCostFlowBase.Status solveStatus = minCostFlow.solve();
53 assertEquals(solveStatus, MinCostFlow.Status.OPTIMAL);
54 final long totalFlowCost = minCostFlow.getOptimalCost();
55 assertEquals(expectedCost, totalFlowCost);
60 final int numNodes = 6;
61 final int numArcs = 9;
62 final int[] tails = {0, 0, 0, 0, 1, 2, 3, 3, 4};
63 final int[] heads = {1, 2, 3, 4, 3, 4, 4, 5, 5};
64 final int[] capacities = {5, 8, 2, 0, 4, 5, 6, 0, 4};
65 final int[] expectedFlows = {4, 4, 2, 0, 4, 4, 0, 6, 4};
66 final int expectedTotalFlow = 10;
67 final MaxFlow maxFlow =
new MaxFlow();
68 assertNotNull(maxFlow);
69 for (
int i = 0; i < numArcs; ++i) {
70 maxFlow.addArcWithCapacity(tails[i], heads[i], capacities[i]);
72 maxFlow.setArcCapacity(7, 6);
73 final MaxFlow.Status solveStatus = maxFlow.solve(0, numNodes - 1);
74 assertEquals(solveStatus, MaxFlow.Status.OPTIMAL);
75 final long totalFlow = maxFlow.getOptimalFlow();
76 assertEquals(expectedTotalFlow, totalFlow);
77 for (
int i = 0; i < numArcs; ++i) {
78 assertEquals(maxFlow.getFlow(i), expectedFlows[i]);