Opening your workspace…
Workspace/Coding room
Opening your workspace…
Every street on the brewery delivery map is one-way, thanks to an unfortunate meeting between two handcarts and a wedding cake. The n stops are numbered 0 through n - 1, and each entry in edges is [from, to, cost], a road with a nonnegative toll. Return the lowest total toll for a journey from start to end. Return -1 if no route reaches end. If the parcel is already at its destination, the bill is zero.
4, [[0,1,4],[0,2,1],[2,1,1],[1,3,2]], 0, 3Output4The cheapest route is 0 → 2 → 1 → 3, which costs 1 + 1 + 2 = 4.
3, [[0,1,2]], 0, 2Output-1No road leads to stop 2, so it can't be reached.
No cost is negative, so the unvisited stop with the smallest known distance already has its final distance. Take that stop, update the distances of the stops its roads lead to, and repeat.
First practice exploring reachable vertices without weighted distances.
Try this problem →Class C · Arrays / Two pointersCompare selecting the next smallest queue entry with selecting a tentative distance.
Try this problem →/** @param {number} n * @param {number[][]} edges * @param {number} start * @param {number} end * @returns {number} */ function solve(n, edges, start, end) { // Follow the cheapest available route. }
Tab inserts two spaces. Shift+Tab outdents. Use the arrow keys and Tab to choose a suggestion. Press Escape, then Tab, to leave the editor. Press Control or Command with apostrophe to run samples or with Enter to submit.
4, [[0,1,4],[0,2,1],[2,1,1],[1,3,2]], 0, 34