Compute shortest path in cyclic graph
Company: Microsoft
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
## Problem: Shortest Path in a Graph With Cycles
You are given a directed weighted graph that may contain cycles.
- There are `n` nodes labeled `0..n-1`.
- You are given an edge list `edges`, where each edge is `(u, v, w)` meaning there is a directed edge from `u` to `v` with non-negative weight `w`.
- You are also given a source node `s` and a target node `t`.
### Task
Return the length of the shortest path from `s` to `t`. If `t` is not reachable from `s`, return `-1`.
### Input/Output
- **Input:** `n`, `edges`, `s`, `t`
- **Output:** shortest distance from `s` to `t` or `-1`
### Constraints (assume for interview)
- `1 <= n <= 2 * 10^5`
- `0 <= |edges| <= 3 * 10^5`
- `0 <= w <= 10^9`
- Graph can contain self-loops and cycles
### Example
- `n = 5`
- `edges = [(0,1,2),(1,2,3),(2,1,1),(2,3,4),(0,4,10)]`
- `s = 0, t = 3`
Shortest path: `0 -> 1 -> 2 -> 3` with total weight `2 + 3 + 4 = 9`.
So output is `9`.
Quick Answer: This question evaluates understanding of graph algorithms and shortest-path computation in weighted directed graphs, including handling cycles, non-negative edge weights, self-loops, and related edge-case reasoning.