Question bank › Graphs Bfs
Dsa Graphs Bfs Easy

Path Length in Network

You are given a directed graph representing a network of roads between various locations. Each road has an associated travel time. The input consists of the number of locations, the number of roads, a list of roads defined by their starting location, ending location, and travel time, and finally the starting and ending locations for which you want to find the shortest travel time. Implement a function that calculates the shortest travel time from the starting location to the ending location using BFS. Input format: N R A1 B1 T1 A2 B2 T2 ... AR BR TR S E Where N is the number of locations (1 <= N <= 100), R is the number of roads (1 <= R <= 500), and each road is defined by A B T where A and B are locations and T is the travel time. S and E are the starting and ending locations. Output format: Print the shortest travel time from S to E or -1 if there is no path. Example: Input: 5 6 1 2 3 1 3 4 2 3 1 2 4 2 3 4 1 4 5 3 1 5 Output: 10

Key concepts

graphsbfsshortest-path

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
Part of InterviewLab's verified interview question bank. We show the prompt and concepts to practise with — never a copy-paste solution.