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 (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
🏠 Start🎯 TargetExpress EntryExpress ExitDirect: 700 units200Express: 50282Optimal Path: 200 + 50 + 282 = 532 unitsExpress lane saves 168 units compared to direct route!
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)

n
2n
⚑ Linearithmic
Space Complexity
O(VΒ²)

Storing the graph adjacency information and distance arrays

n
2n
βœ“ 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
Asked in
Google 15 Amazon 12 Meta 8 Microsoft 6
28.0K Views
Medium Frequency
~25 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
πŸ’‘ Explanation
AI Ready
πŸ’‘ Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen