Leetcode Problem 2737. Find the Closest Marked Node
2737. Find the Closest Marked Node
Leetcode Solutions
Dijkstra's Algorithm for Shortest Path
Initialize a priority queue (min-heap) and a distance array with infinity values except for the source node s which is set to 0.
Add the source node s to the priority queue with a distance of 0.
While the priority queue is not empty:
a. Pop the node with the smallest distance from the priority queue.
b. If the node is marked, return its distance as the result.
c. Otherwise, iterate through its adjacent nodes.
d. For each adjacent node, if the distance through the current node is shorter, update the distance and add the adjacent node to the priority queue.