Lv.3dijkstragraphheapshortest-path
Fastest Delivery Routes
- Memory limit
- 256 MB
- Correctness time limit
- 10000 ms
- Solvers
- 0
There are n towns 0..n-1; the warehouse is in town start. roads[i] = [a, b, t] is a two-way road taking t
minutes. Return an array whose i-th element is the shortest travel time from start to town i (-1 if unreachable,
0 for start).
Examples
| n | roads | start | result |
|---|---|---|---|
4 | [[0, 1, 4], [0, 2, 1], [2, 1, 2], [1, 3, 5]] | 0 | [0, 3, 1, 8] |
3 | [[0, 1, 7]] | 1 | [7, 0, -1] |
Efficiency
This problem has performance tests. Judge the required time complexity from the input sizes in the constraints.
Function signature
solution(n: int, roads: int[][], start: int) → long[]
Constraints
-
n,start,roads.length - , ; parallel roads and self-loops may appear. Distances can exceed 32 bits.
Expected complexity
- Time
- O((N + M) log N)
- Space
- O(N + M)
Examples
| n | roads | start | Expected |
|---|---|---|---|
4 | [[0,1,4],[0,2,1],[2,1,2],[1,3,5]] | 0 | [0,3,1,8] |
3 | [[0,1,7]] | 1 | [7,0,-1] |
Hint
Hint
Dijkstra with a min-heap: pop the closest (d, u), skip stale entries with d > dist[u], relax neighbours. Use 64-bit
distances. Scanning all towns for the closest unsettled one is and exceeds the time limit.
Time limits per language
| Language | Efficiency time limit | |
|---|---|---|
| C (GCC, gnu17) | 292 ms | estimated |
| C++ (GCC, gnu++23) | 292 ms | derived |
| Python 3.13 | 1000 ms | derived |
| Java 25 | 1816 ms | derived |
| Kotlin 2.4 (JVM) | 1584 ms | estimated |
| C# 14 (.NET 10) | 1584 ms | estimated |
Test groups
| Group | Weight | Tests | Visibility |
|---|---|---|---|
| sample | 0 | 2 | inputs and outputs |
| correctness | 40 | 9 | verdict only |
| performance | 60 | 4 | verdict only |