A HACKERBREWERY ORIGINAL
The long way home
HardGraphsShortest paths
Plan a delivery through a directed network of n stops, numbered 0 to n − 1. Each edge is [from, to, cost], with a nonnegative cost. Return the cheapest total cost from start to end, or −1 if no route exists. Traveling from a stop to itself costs zero.
Example 1
INPUT
4, [[0,1,4],[0,2,1],[2,1,1],[1,3,2]], 0, 3OUTPUT4Travel 0 → 2 → 1 → 3, for a total cost of 1 + 1 + 2.
Example 2
INPUT
3, [[0,1,2]], 0, 2OUTPUT-1There is no route to stop 2.
Keep in mind
- 1 ≤ n ≤ 1,000
- 0 ≤ edges.length ≤ 5,000
- 0 ≤ cost ≤ 10,000
- start and end are valid stop numbers.
Draft saved on this browserSpaces: 2 UTF-8
INPUT
4, [[0,1,4],[0,2,1],[2,1,1],[1,3,2]], 0, 3EXPECTED OUTPUT
4Public samples only. These checks do not award ranked points.