Minimum Cost of a Path With Special Roads - Problem
Navigate the 2D Grid with Special Shortcuts!
Imagine you're a delivery driver in a city with a unique transportation system. You start at position
π Normal Movement: You can move anywhere in the 2D space, but the cost is the Manhattan distance:
π£οΈ Special Roads: The city has special one-way express routes! Each special road is defined as
π― Your Goal: Find the minimum cost to travel from start to target, using any combination of normal movement and special roads (you can use each special road multiple times).
This is essentially a shortest path problem in a graph where every point can connect to every other point, plus some special edges with custom costs!
Imagine you're a delivery driver in a city with a unique transportation system. You start at position
(startX, startY) and need to reach your destination at (targetX, targetY).π Normal Movement: You can move anywhere in the 2D space, but the cost is the Manhattan distance:
|x2 - x1| + |y2 - y1|π£οΈ Special Roads: The city has special one-way express routes! Each special road is defined as
[x1, y1, x2, y2, cost] - you can travel from (x1, y1) to (x2, y2) for exactly cost units. These might be faster or slower than normal movement!π― Your Goal: Find the minimum cost to travel from start to target, using any combination of normal movement and special roads (you can use each special road multiple times).
This is essentially a shortest path problem in a graph where every point can connect to every other point, plus some special edges with custom costs!
Input & Output
example_1.py β Basic Case
$
Input:
start = [1,1], target = [4,5], specialRoads = [[1,2,3,3,2],[3,4,4,5,1]]
βΊ
Output:
5
π‘ Note:
Optimal path: (1,1) β (1,2) β use special road to (3,3) β (3,4) β use special road to (4,5). Cost = 1 + 2 + 1 + 1 = 5. Direct path would cost |4-1| + |5-1| = 7.
example_2.py β No Special Roads Used
$
Input:
start = [3,2], target = [5,7], specialRoads = [[3,2,3,4,4],[3,3,5,5,5]]
βΊ
Output:
7
π‘ Note:
Direct Manhattan distance is |5-3| + |7-2| = 7. The special roads are more expensive than direct movement, so we don't use them.
example_3.py β Multiple Special Roads
$
Input:
start = [1,1], target = [10,10], specialRoads = [[1,1,5,5,3],[5,5,10,10,3]]
βΊ
Output:
6
π‘ Note:
Use both special roads: (1,1) β special road β (5,5) β special road β (10,10). Cost = 3 + 3 = 6. Direct path would cost 18.
Visualization
Tap to expand
Understanding the Visualization
1
Mark Important Locations
Identify key points: start, destination, and express lane entrances/exits
2
Calculate All Routes
For each important location, calculate cost to reach every other location
3
Add Express Lanes
Some routes have express lanes with different costs than normal driving
4
Find Shortest Path
Use Dijkstra's algorithm to find the minimum cost route
Key Takeaway
π― Key Insight: Transform the infinite 2D space into a finite graph of important points, then use Dijkstra's algorithm to find the shortest path efficiently.
Time & Space Complexity
Time Complexity
O(VΒ² log V)
V is the number of important points (at most 2 + 2*n). For each vertex, we may relax all other vertices, and priority queue operations take O(log V)
β‘ Linearithmic
Space Complexity
O(VΒ²)
Storing the graph adjacency information and distance arrays
β Linear Space
Constraints
- start.length == target.length == 2
- 1 β€ specialRoads.length β€ 200
- specialRoads[i].length == 5
- 1 β€ start[i], target[i], specialRoads[i][j] β€ 105
- Each special road is one-way and can be used multiple times
π‘
Explanation
AI Ready
π‘ Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code