Advertisement

Minimum Cost of a Path With Special Roads - LeetCode 2662 Solution

Minimum Cost of a Path With Special Roads - Complete Solution Guide

Minimum Cost of a Path With Special Roads is LeetCode problem 2662, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

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 ] in

Detailed Explanation

The problem asks us to find the minimum cost to travel from a starting point `(startX, startY)` to a target point `(targetX, targetY)` in a 2D space. The cost of moving between any two points `(x1, y1)` and `(x2, y2)` is the Manhattan distance `|x2 - x1| + |y2 - y1|`. We are also given a set of special roads, where each special road `[x1_i, y1_i, x2_i, y2_i, cost_i]` allows us to travel from `(x1_i, y1_i)` to `(x2_i, y2_i)` at a cost of `cost_i`. We can use each special road any number of times. The goal is to determine the minimum total cost to reach the target from the starting point.

Solution Approach

The provided solution uses Dijkstra's algorithm to find the minimum cost path. It first creates a graph where the nodes are the start point, the target point, and the beginning and end points of each special road. The edges are the Manhattan distances between all pairs of points, and the cost of the special roads are added as edges between the corresponding points. Dijkstra's algorithm then finds the shortest path from the start node to the target node in this graph using a priority queue to efficiently select the next node to explore. During the algorithm, the direct Manhattan distance to the target is also considered from each node to further improve efficiency.

Step-by-Step Algorithm

  1. Step 1: Create a list of all unique points: the starting point, the target point, and the starting and ending points of all special roads.
  2. Step 2: Construct a graph represented by a cost matrix. The cost between two nodes is the Manhattan distance between them.
  3. Step 3: Add the special road costs to the cost matrix. If a special road connects two points, the cost between those points is updated to the minimum of the Manhattan distance and the special road cost.
  4. Step 4: Initialize distances to all nodes as infinity, except for the starting node, which has a distance of 0.
  5. Step 5: Use a priority queue to store nodes to visit, ordered by their distance from the starting node.
  6. Step 6: While the priority queue is not empty, extract the node with the minimum distance. If this node is the target, return the distance.
  7. Step 7: For each neighbor of the current node, calculate the distance to that neighbor through the current node. If this distance is less than the current distance to the neighbor, update the neighbor's distance and add it to the priority queue.
  8. Step 8: If the target is never reached in the main loop, it means some error has occurred (but given the problem constraints, it's theoretically impossible if graph is constructed correctly) . Return the calculated distance to the target.

Key Insights

  • Insight 1: The problem can be modeled as a graph where nodes are the start point, target point, and the start and end points of all special roads. Edges are either Manhattan distances between points or the cost of special roads.
  • Insight 2: Dijkstra's algorithm is suitable for finding the shortest path from the starting point to the target point in this graph, as it guarantees finding the minimum cost path in a weighted graph with non-negative edge weights.
  • Insight 3: An optimization can be made by considering the direct Manhattan distance to the target from any intermediate node during Dijkstra's search, allowing the algorithm to terminate earlier or find a shorter path.

Complexity Analysis

Time Complexity: O(N * M * log(N * M))

Space Complexity: O(N * M)

Topics

This problem involves: Array, Graph, Heap (Priority Queue), Shortest Path.

Companies

Asked at: Samsung.