|
| 1 | +# Minimum Cost Maximum Flow |
| 2 | + |
| 3 | +## English |
| 4 | + |
| 5 | +The minimum cost maximum flow problem is a fundamental problem in network flow theory, combining flow maximization with cost minimization. The Bellman-Ford algorithm is used to find shortest paths in graphs with negative edge weights, enabling efficient cost optimization. |
| 6 | + |
| 7 | +This challenge involves implementing an algorithm that finds the maximum flow with the minimum cost in a network. |
| 8 | + |
| 9 | +### Relevant Code Snippet |
| 10 | + |
| 11 | +```javascript |
| 12 | +class MinCostMaxFlow { |
| 13 | + constructor(n) { |
| 14 | + this.n = n; |
| 15 | + this.graph = Array.from({ length: n }, () => []); |
| 16 | + } |
| 17 | + |
| 18 | + addEdge(u, v, capacity, cost) { |
| 19 | + this.graph[u].push([v, capacity, cost, this.graph[v].length]); |
| 20 | + this.graph[v].push([u, 0, -cost, this.graph[u].length - 1]); |
| 21 | + } |
| 22 | + |
| 23 | + bellmanFord(source, dist, parent) { |
| 24 | + dist.fill(Infinity); |
| 25 | + dist[source] = 0; |
| 26 | + const inQueue = new Array(this.n).fill(false); |
| 27 | + const queue = [source]; |
| 28 | + inQueue[source] = true; |
| 29 | + |
| 30 | + while (queue.length > 0) { |
| 31 | + const u = queue.shift(); |
| 32 | + inQueue[u] = false; |
| 33 | + for (let i = 0; i < this.graph[u].length; i++) { |
| 34 | + const [v, capacity, cost, rev] = this.graph[u][i]; |
| 35 | + if (capacity > 0 && dist[u] + cost < dist[v]) { |
| 36 | + dist[v] = dist[u] + cost; |
| 37 | + parent[v] = [u, i]; |
| 38 | + if (!inQueue[v]) { |
| 39 | + queue.push(v); |
| 40 | + inQueue[v] = true; |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + minCostMaxFlow(source, sink) { |
| 48 | + let flow = 0; |
| 49 | + let cost = 0; |
| 50 | + const dist = new Array(this.n); |
| 51 | + const parent = new Array(this.n).fill([-1, -1]); |
| 52 | + |
| 53 | + while (true) { |
| 54 | + this.bellmanFord(source, dist, parent); |
| 55 | + if (dist[sink] === Infinity) { |
| 56 | + break; |
| 57 | + } |
| 58 | + |
| 59 | + let pathFlow = Infinity; |
| 60 | + let v = sink; |
| 61 | + while (v !== source) { |
| 62 | + const [u, i] = parent[v]; |
| 63 | + pathFlow = Math.min(pathFlow, this.graph[u][i][1]); |
| 64 | + v = u; |
| 65 | + } |
| 66 | + |
| 67 | + flow += pathFlow; |
| 68 | + cost += pathFlow * dist[sink]; |
| 69 | + |
| 70 | + v = sink; |
| 71 | + while (v !== source) { |
| 72 | + const [u, i] = parent[v]; |
| 73 | + this.graph[u][i][1] -= pathFlow; |
| 74 | + const rev = this.graph[u][i][3]; |
| 75 | + this.graph[v][rev][1] += pathFlow; |
| 76 | + v = u; |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + return { flow, cost }; |
| 81 | + } |
| 82 | +} |
| 83 | +``` |
| 84 | + |
| 85 | +### History |
| 86 | + |
| 87 | +The minimum cost maximum flow problem is a classic combinatorial optimization problem with applications in transportation, network design, and resource allocation. |
| 88 | + |
| 89 | +--- |
| 90 | + |
| 91 | +## Español |
| 92 | + |
| 93 | +Flujo Máximo de Costo Mínimo |
| 94 | + |
| 95 | +El problema de flujo máximo de costo mínimo es un problema fundamental en la teoría de flujos en redes, que combina la maximización del flujo con la minimización del costo. El algoritmo de Bellman-Ford se usa para encontrar caminos más cortos en grafos con pesos negativos, permitiendo una optimización eficiente del costo. |
| 96 | + |
| 97 | +Este reto consiste en implementar un algoritmo que encuentre el flujo máximo con el costo mínimo en una red. |
| 98 | + |
| 99 | +### Fragmento de Código Relevante |
| 100 | + |
| 101 | +```javascript |
| 102 | +class MinCostMaxFlow { |
| 103 | + constructor(n) { |
| 104 | + this.n = n; |
| 105 | + this.graph = Array.from({ length: n }, () => []); |
| 106 | + } |
| 107 | + |
| 108 | + addEdge(u, v, capacity, cost) { |
| 109 | + this.graph[u].push([v, capacity, cost, this.graph[v].length]); |
| 110 | + this.graph[v].push([u, 0, -cost, this.graph[u].length - 1]); |
| 111 | + } |
| 112 | + |
| 113 | + bellmanFord(source, dist, parent) { |
| 114 | + dist.fill(Infinity); |
| 115 | + dist[source] = 0; |
| 116 | + const inQueue = new Array(this.n).fill(false); |
| 117 | + const queue = [source]; |
| 118 | + inQueue[source] = true; |
| 119 | + |
| 120 | + while (queue.length > 0) { |
| 121 | + const u = queue.shift(); |
| 122 | + inQueue[u] = false; |
| 123 | + for (let i = 0; i < this.graph[u].length; i++) { |
| 124 | + const [v, capacity, cost, rev] = this.graph[u][i]; |
| 125 | + if (capacity > 0 && dist[u] + cost < dist[v]) { |
| 126 | + dist[v] = dist[u] + cost; |
| 127 | + parent[v] = [u, i]; |
| 128 | + if (!inQueue[v]) { |
| 129 | + queue.push(v); |
| 130 | + inQueue[v] = true; |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + minCostMaxFlow(source, sink) { |
| 138 | + let flow = 0; |
| 139 | + let cost = 0; |
| 140 | + const dist = new Array(this.n); |
| 141 | + const parent = new Array(this.n).fill([-1, -1]); |
| 142 | + |
| 143 | + while (true) { |
| 144 | + this.bellmanFord(source, dist, parent); |
| 145 | + if (dist[sink] === Infinity) { |
| 146 | + break; |
| 147 | + } |
| 148 | + |
| 149 | + let pathFlow = Infinity; |
| 150 | + let v = sink; |
| 151 | + while (v !== source) { |
| 152 | + const [u, i] = parent[v]; |
| 153 | + pathFlow = Math.min(pathFlow, this.graph[u][i][1]); |
| 154 | + v = u; |
| 155 | + } |
| 156 | + |
| 157 | + flow += pathFlow; |
| 158 | + cost += pathFlow * dist[sink]; |
| 159 | + |
| 160 | + v = sink; |
| 161 | + while (v !== source) { |
| 162 | + const [u, i] = parent[v]; |
| 163 | + this.graph[u][i][1] -= pathFlow; |
| 164 | + const rev = this.graph[u][i][3]; |
| 165 | + this.graph[v][rev][1] += pathFlow; |
| 166 | + v = u; |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + return { flow, cost }; |
| 171 | + } |
| 172 | +} |
| 173 | +``` |
| 174 | + |
| 175 | +### Historia |
| 176 | + |
| 177 | +El problema de flujo máximo de costo mínimo es un problema clásico de optimización combinatoria con aplicaciones en transporte, diseño de redes y asignación de recursos. |
0 commit comments