1066. Campus Bikes II
On a campus represented as a 2D grid, there are Nworkers and Mbikes, with N <= M. Each worker and bike is a 2D coordinate on this grid.
We assign one unique bike to each worker so that the sum of the Manhattan distances between each worker and their assigned bike is minimized.
The Manhattan distance between two points p1and p2is Manhattan(p1, p2) = |p1.x - p2.x| + |p1.y - p2.y|.
Return the minimum possible sum of Manhattan distances between each worker and their assigned bike.
Example 1:

Example 2:

Note:
0 <= workers[i][0], workers[i][1], bikes[i][0], bikes[i][1] < 1000All worker and bike locations are distinct.
1 <= workers.length <= bikes.length <= 10
Thoughts:
Dijkstra on augmented path: vertex: states of chosen bikes, edge: Manhattan distance between worker i and bike j (Original Post)
Minimum-cost flow (Original Post): 1. Construct the graph by attaching the source and sink node along with workers and bikes node (vertex). 2. Construct the edge of the graph with capacity and cost connecting two nodes (vertices). 3. Iteratively sending flow 1 to the graph: 1. Using Bellman-ford to find the shortest path between on source and sink on current graph connection 2. Update the graph connection by augmenting path update as subtracting 1 (flow) from forward edges and add 1 for reverse edges for each node. 3. Adding the current flow cost to the answer. 4. Finally return the answer
DFS with early pruning (Original Post)
DFS with memorization (Original Post)
bitmasking DP (Original Post)
Code: Dijkstra on augmented path:
Code: Minumum-cost flow: (Fav) T:((m * n) * m) : [O(|E|f)]
Code: DFS with early pruning:
Last updated
Was this helpful?