Dijkstra · Medium
A route explorer lists every destination reachable within a total cost budget. The menu is ordered by cheapest cost first and node label second. It must include the starting node when the budget allows zero and exclude unreachable destinations even when the budget is large.
Return reachable node labels whose minimum route cost does not exceed budget ordered by minimum cost and then numeric label.
Input
Call
- Implement solve(input) and return the result. The provided adapter handles JSON input and output.
Input shape
{ n: integer, edges: [integer, integer, integer][], source: integer, budget: integer }
Fields
- n is the node count and edges contains directed [from,to,weight] triples. Parallel edges are distinct input entries; self-loops are allowed.
- All weights are non-negative integers unless the task explicitly requires positive weights.
- source is the starting node. source = -1 means no starting node and yields no reachable nodes.
- budget is the maximum allowed total route cost and may be negative.
Output
Return value
- Return node labels, not costs or routes. Include source at cost zero when source exists and budget >= 0.
- A negative budget or missing source returns []. Distances equal to budget qualify.
Constraints
- 0 <= n <= 50000; 0 <= edges.length <= 200000; endpoints are valid node labels; 0 <= weight <= 1000000.
- source and target, when used, are either -1 or valid node labels. All distance and slack results fit a safe JSON integer.
- -1 <= budget <= 50000000000.
Examples
Example 1
{
"budget": 3,
"edges": [
[
0,
2,
3
],
[
0,
1,
3
],
[
2,
3,
1
]
],
"n": 4,
"source": 0
}Returns
[
0,
1,
2
]Example 2
{
"budget": -1,
"edges": [],
"n": 1,
"source": 0
}Returns
[]Required complexity
O(n log(n + 1) + m log(m + 1)) time and O(n + m) auxiliary space