Dsa
Graphs Bfs
Medium
Network Delay Time
You are given a number of nodes n and a list of directed edges in the format (u, v, w) where u can reach v with the delay time w. Determine the minimum time it takes to reach all nodes from a starting node k. If any node is unreachable, return -1.
Input format: The first line contains three integers n, m (1 <= n <= 100, 1 <= m <= 1000) and k, representing the number of nodes, number of edges, and the starting node respectively. The next m lines contain three integers u, v, w.
Output format: An integer representing the minimum time to reach all nodes or -1 if it is not possible.
Example:
Input:
4 4 2
1 2 1
1 3 3
2 3 1
3 4 1
Output:
4
Key concepts
graphsbfsdijkstra's algorithm
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now