Leetcode Problem 2737. Find the Closest Marked Node

2737. Find the Closest Marked Node

Leetcode Solutions

Dijkstra's Algorithm for Shortest Path

  1. Initialize a priority queue (min-heap) and a distance array with infinity values except for the source node s which is set to 0.
  2. Add the source node s to the priority queue with a distance of 0.
  3. 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.
  4. If no marked node is reached, return -1.
UML Thumbnail

Bellman-Ford Algorithm for Shortest Path

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...