Find the minimum time needed to travel from a source node to a destination node in a directed graph with weighted edges.
Problem
You are given a directed graph with n nodes labeled from 0 to n - 1. Each directed edge has a travel time. Starting from a source node, determine the minimum time required to reach a destination node.
If the destination cannot be reached, return -1.
The graph may contain multiple outgoing edges from a node, and edge times are non-negative.
Goal
Compute the smallest possible total travel time from source to destination.
Input Format
n: number of nodes.edges: list of directed weighted edges, where each edge is typically represented as[u, v, w]meaning a directed edge fromutovwith travel timew.source: starting node.destination: target node.
Output Format
Return the minimum total time to reach destination from source, or -1 if no path exists.
Constraints
1 <= n- Nodes are labeled
0throughn - 1. - Edge weights are non-negative.
- The graph can be disconnected.
- Multiple paths may exist between the same pair of nodes.
Example 1
Input
n = 5, edges = [[0,1,4],[0,2,1],[2,1,2],[1,3,1],[2,3,5],[3,4,3]], source = 0, destination = 4
Output
7
Explanation
The best route is 0 -> 2 -> 1 -> 3 -> 4 with total time 1 + 2 + 1 + 3 = 7.
Example 2
Input
n = 4, edges = [[0,1,2],[1,2,2]], source = 0, destination = 3
Output
-1
Explanation
Node 3 is unreachable from node 0.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.