✦ Robotics · graph algorithms
Shortest route for a delivery robot
Use Dijkstra's algorithm to find the cheapest route between rooms connected by corridors.
- Level 5 · Research Engineer
- 6 tests
- technically checked
Your task
A warehouse is a graph: rooms are nodes 0 … n − 1, corridors are undirected edges [a, b, metres]. Dijkstra's algorithm finds the shortest distance from a start node by always expanding the closest unvisited node.
Write shortestPath(n, edges, from, to) returning the shortest distance, or −1 if to cannot be reached. Distance from a node to itself is 0.
Hints (0 of 3 shown)
What gets checked
- shortestPath(4, [[0, 1, 4], [0, 2, 1], [2, 1, 2], [1, 3, 1], [2, …, 0, 3) → 4
- shortestPath(4, [[0, 1, 4], [0, 2, 1], [2, 1, 2], [1, 3, 1], [2, …, 3, 0) → 4
Plus 4 hidden tests for edge cases. They run in the sandbox like the others; only the result is shown.
Tab indents · Esc then Tab leaves the editor · Ctrl/⌘ Enter runs · Ctrl/⌘ Shift Enter checks · Ctrl/⌘ / comments
Press Run to see what your program prints.
Your code runs only in this browser, inside an isolated sandbox with no network access, and stops after 3 s. Nothing you type is sent to Tactiverge; drafts and progress are saved in this browser only.