Minimum Cost of a Path With Special Roads Problem
Description
LeetCode Problem 2662.
You are given an array start where start = [startX, startY] represents your initial position (startX, startY) in a 2D space. You are also given the array target where target = [targetX, targetY] represents your target position (targetX, targetY).
The cost of going from a position (x1, y1) to any other position in the space (x2, y2) is |x2 - x1| + |y2 - y1|.
There are also some special roads. You are given a 2D array specialRoads where specialRoads[i] = [x1_i, y1_i, x2_i, y2_i, cost_i] indicates that the i^th special road goes in one direction from (x1_i, y1_i) to (x2_i, y2_i) with a cost equal to cost_i. You can use each special road any number of times.
Return the minimum cost required to go from (startX, startY) to (targetX, targetY).
Example 1:
Input: start = [1,1], target = [4,5], specialRoads = [[1,2,3,3,2],[3,4,4,5,1]]
Output: 5
Explanation:
1. (1,1) to (1,2) with a cost of |1 - 1| + |2 - 1| = 1.
2. (1,2) to (3,3). Use specialRoads[0] with the cost 2.
3. (3,3) to (3,4) with a cost of |3 - 3| + |4 - 3| = 1.
4. (3,4) to (4,5). Use specialRoads[1] with the cost 1.
So the total cost is 1 + 2 + 1 + 1 = 5.
Example 2:
Input: start = [3,2], target = [5,7], specialRoads = [[5,7,3,2,1],[3,2,3,4,4],[3,3,5,5,5],[3,4,5,6,6]]
Output: 7
Explanation:
It is optimal not to use any special edges and go directly from the starting to the ending position with a cost |5 - 3| + |7 - 2| = 7.
Note that the specialRoads[0] is directed from (5,7) to (3,2).
Example 3:
Input: start = [1,1], target = [10,4], specialRoads = [[4,2,1,1,3],[1,2,7,4,4],[10,3,6,1,2],[6,1,1,2,3]]
Output: 8
Explanation:
1. (1,1) to (1,2) with a cost of |1 - 1| + |2 - 1| = 1.
2. (1,2) to (7,4). Use specialRoads[1] with the cost 4.
3. (7,4) to (10,4) with a cost of |10 - 7| + |4 - 4| = 3.
Constraints:
start.length == target.length == 21 <= startX <= targetX <= 10^51 <= startY <= targetY <= 10^51 <= specialRoads.length <= 200specialRoads[i].length == 5startX <= x1_i, x2_i <= targetXstartY <= y1_i, y2_i <= targetY1 <= cost_i <= 10^5
Solution
Approach: Dijkstra
The interesting points are the start and the ends of special roads. From a point (x, y) reached with cost d, going straight to the target costs d + |x - targetX| + |y - targetY|, and taking a special road (x1, y1) -> (x2, y2) costs |x - x1| + |y - y1| + cost to land on (x2, y2).
Run Dijkstra with a priority queue of (d, x, y). Each time a new point is popped, update the answer with the direct trip to the target, then push the result of using every special road. The answer is the best value seen when the queue is empty.
Complexity: O(n^2 log n) time and O(n^2) space, where n is the number of special roads.
from heapq import heappop, heappush
from math import inf
class Solution:
def minimumCost(
self, start: List[int], target: List[int], specialRoads: List[List[int]]
) -> int:
def dist(x1: int, y1: int, x2: int, y2: int) -> int:
return abs(x1 - x2) + abs(y1 - y2)
q = [(0, start[0], start[1])]
vis = set()
ans = inf
while q:
d, x, y = heappop(q)
if (x, y) in vis:
continue
vis.add((x, y))
ans = min(ans, d + dist(x, y, *target))
for x1, y1, x2, y2, cost in specialRoads:
heappush(q, (d + dist(x, y, x1, y1) + cost, x2, y2))
return ans