{"id":"6b093dddbd4755deb67ee064","slug":"dijkstra-shortest-paths","title":"Shortest delivery times (Dijkstra)","summary":"A network of logistics hubs with one-way routes of different travel times. Compute the shortest travel time from the warehouse to every hub – Dijkstra's algorithm with a heap that some languages make you write yourself.","servedLang":"en","availableLangs":["pl","en"],"kind":"code","category":"algorithms","status":"published","difficulty":"hard","topics":[],"techs":["typescript","javascript","python","dotnet","java","go"],"tags":["graphs","dijkstra","heap","shortest-path","priority-queue"],"languages":[{"lang":"typescript","runner":"browser","starter":"/**\n * Shortest travel time from source to every hub over directed weighted edges [from, to, minutes];\n * -1 for unreachable hubs.\n */\nfunction shortestDistances(n: number, edges: [number, number, number][], source: number): number[] {\n  // TODO\n  throw new Error('Not implemented');\n}\n","testCount":7,"tests":"test('basic network', () => {\n  expect(shortestDistances(4, [[0, 1, 4], [0, 2, 1], [2, 1, 2], [1, 3, 1], [2, 3, 5]], 0)).toEqual([0, 3, 1, 4]);\n});\n\ntest('more hops can be faster', () => {\n  expect(shortestDistances(4, [[0, 3, 10], [0, 1, 1], [1, 2, 1], [2, 3, 1]], 0)).toEqual([0, 1, 2, 3]);\n});\n\ntest('routes are one-way, unreachable hubs give -1', () => {\n  expect(shortestDistances(3, [[1, 0, 5], [0, 2, 7]], 0)).toEqual([0, -1, 7]);\n  expect(shortestDistances(3, [], 1)).toEqual([-1, 0, -1]);\n});\n\ntest('zero weights, parallel routes and self-loops', () => {\n  expect(shortestDistances(3, [[0, 1, 5], [0, 1, 2], [1, 1, 0], [1, 2, 0]], 0)).toEqual([0, 2, 2]);\n});\n\ntest('any hub can be the source', () => {\n  expect(shortestDistances(5, [[3, 4, 2], [4, 0, 1], [0, 3, 9]], 3)).toEqual([3, -1, -1, 0, 2]);\n});\n\ntest('large totals do not overflow', () => {\n  const n = 100001;\n  const edges = Array.from({ length: n - 1 }, (_, i) => [i, i + 1, 1000000]);\n  const dist = shortestDistances(n, edges, 0);\n  expect(dist[n - 1]).toBe(100000000000);\n  expect(dist[50000]).toBe(50000000000);\n});\n\ntest('performance: 50 000 hubs, 200 000 routes', () => {\n  const n = 50000;\n  const edges = [];\n  for (let i = 0; i < n - 1; i++) edges.push([i, i + 1, ((i * 7919) % 1000) + 1]);\n  for (let j = 0; j < 150000; j++) edges.push([(j * 7919) % n, (j * 104729 + 13) % n, ((j * 31337) % 100000) + 1]);\n  const dist = shortestDistances(n, edges, 0);\n  expect(dist.reduce((a, b) => a + b, 0)).toBe(2005525985);\n  expect([dist[n - 1], dist[n / 2], dist[12345]]).toEqual([26962, 39700, 38764]);\n});\n"},{"lang":"javascript","runner":"browser","starter":"/**\n * Shortest travel time from source to every hub over directed weighted edges [from, to, minutes];\n * -1 for unreachable hubs.\n * @param {number} n\n * @param {number[][]} edges\n * @param {number} source\n * @returns {number[]}\n */\nfunction shortestDistances(n, edges, source) {\n  // TODO\n  throw new Error('Not implemented');\n}\n","testCount":7,"tests":"test('basic network', () => {\n  expect(shortestDistances(4, [[0, 1, 4], [0, 2, 1], [2, 1, 2], [1, 3, 1], [2, 3, 5]], 0)).toEqual([0, 3, 1, 4]);\n});\n\ntest('more hops can be faster', () => {\n  expect(shortestDistances(4, [[0, 3, 10], [0, 1, 1], [1, 2, 1], [2, 3, 1]], 0)).toEqual([0, 1, 2, 3]);\n});\n\ntest('routes are one-way, unreachable hubs give -1', () => {\n  expect(shortestDistances(3, [[1, 0, 5], [0, 2, 7]], 0)).toEqual([0, -1, 7]);\n  expect(shortestDistances(3, [], 1)).toEqual([-1, 0, -1]);\n});\n\ntest('zero weights, parallel routes and self-loops', () => {\n  expect(shortestDistances(3, [[0, 1, 5], [0, 1, 2], [1, 1, 0], [1, 2, 0]], 0)).toEqual([0, 2, 2]);\n});\n\ntest('any hub can be the source', () => {\n  expect(shortestDistances(5, [[3, 4, 2], [4, 0, 1], [0, 3, 9]], 3)).toEqual([3, -1, -1, 0, 2]);\n});\n\ntest('large totals do not overflow', () => {\n  const n = 100001;\n  const edges = Array.from({ length: n - 1 }, (_, i) => [i, i + 1, 1000000]);\n  const dist = shortestDistances(n, edges, 0);\n  expect(dist[n - 1]).toBe(100000000000);\n  expect(dist[50000]).toBe(50000000000);\n});\n\ntest('performance: 50 000 hubs, 200 000 routes', () => {\n  const n = 50000;\n  const edges = [];\n  for (let i = 0; i < n - 1; i++) edges.push([i, i + 1, ((i * 7919) % 1000) + 1]);\n  for (let j = 0; j < 150000; j++) edges.push([(j * 7919) % n, (j * 104729 + 13) % n, ((j * 31337) % 100000) + 1]);\n  const dist = shortestDistances(n, edges, 0);\n  expect(dist.reduce((a, b) => a + b, 0)).toBe(2005525985);\n  expect([dist[n - 1], dist[n / 2], dist[12345]]).toEqual([26962, 39700, 38764]);\n});\n"},{"lang":"python","runner":"browser","starter":"def shortest_distances(n: int, edges: list[list[int]], source: int) -> list[int]:\n    \"\"\"Shortest travel time from source to every hub over directed weighted edges [from, to, minutes];\n    -1 for unreachable hubs.\"\"\"\n    raise NotImplementedError\n","testCount":7,"tests":"def check(n, edges, source, expected):\n    got = shortest_distances(n, edges, source)\n    assert got == expected, f\"shortest_distances({n}, {edges[:6]}, {source}): expected {expected}, got {got}\"\n\n\ndef test_basic_network():\n    check(4, [[0, 1, 4], [0, 2, 1], [2, 1, 2], [1, 3, 1], [2, 3, 5]], 0, [0, 3, 1, 4])\n\n\ndef test_more_hops_can_be_faster():\n    check(4, [[0, 3, 10], [0, 1, 1], [1, 2, 1], [2, 3, 1]], 0, [0, 1, 2, 3])\n\n\ndef test_routes_are_one_way_unreachable_gives_minus_one():\n    check(3, [[1, 0, 5], [0, 2, 7]], 0, [0, -1, 7])\n    check(3, [], 1, [-1, 0, -1])\n\n\ndef test_zero_weights_parallel_routes_and_self_loops():\n    check(3, [[0, 1, 5], [0, 1, 2], [1, 1, 0], [1, 2, 0]], 0, [0, 2, 2])\n\n\ndef test_any_hub_can_be_the_source():\n    check(5, [[3, 4, 2], [4, 0, 1], [0, 3, 9]], 3, [3, -1, -1, 0, 2])\n\n\ndef test_large_totals():\n    n = 100_001\n    dist = shortest_distances(n, [[i, i + 1, 1_000_000] for i in range(n - 1)], 0)\n    assert dist[-1] == 100_000_000_000 and dist[50_000] == 50_000_000_000, f\"got {dist[-1]}, {dist[50_000]}\"\n\n\ndef test_performance_50k_hubs_200k_routes():\n    n = 50_000\n    edges = [[i, i + 1, (i * 7919) % 1000 + 1] for i in range(n - 1)]\n    edges += [[(j * 7919) % n, (j * 104729 + 13) % n, (j * 31337) % 100000 + 1] for j in range(150_000)]\n    dist = shortest_distances(n, edges, 0)\n    assert sum(dist) == 2005525985, f\"sum of distances: {sum(dist)}\"\n    assert (dist[-1], dist[n // 2], dist[12345]) == (26962, 39700, 38764), \"sample distances\"\n"},{"lang":"csharp","runner":"judge0","starter":"using System;\n\npublic static class Solution\n{\n    /// <summary>\n    /// Shortest travel time from source to every hub over directed weighted edges {from, to, minutes};\n    /// -1 for unreachable hubs.\n    /// </summary>\n    public static long[] ShortestDistances(int n, int[][] edges, int source)\n    {\n        throw new NotImplementedException();\n    }\n}\n","testCount":7},{"lang":"java","runner":"judge0","starter":"class Solution {\n    /**\n     * Shortest travel time from source to every hub over directed weighted edges {from, to, minutes};\n     * -1 for unreachable hubs.\n     */\n    static long[] shortestDistances(int n, int[][] edges, int source) {\n        throw new UnsupportedOperationException(\"Not implemented\");\n    }\n}\n","testCount":7},{"lang":"go","runner":"judge0","starter":"package main\n\n// ShortestDistances returns the shortest travel time from source to every hub over directed\n// weighted edges {from, to, minutes}; -1 for unreachable hubs.\nfunc ShortestDistances(n int, edges [][]int, source int) []int {\n\tpanic(\"not implemented\")\n}\n","testCount":7}],"questionCount":null,"xp":40,"stats":{"attempts":0,"solves":0,"solutions":0,"comments":0},"solveRate":null,"publishedAt":"2026-11-27T08:22:53.776Z","requestedLang":"en","statement":{"type":"doc","content":[{"type":"paragraph","content":[{"type":"text","text":"A courier company has "},{"type":"text","text":"n","marks":[{"type":"code"}]},{"type":"text","text":" hubs (numbered "},{"type":"text","text":"0 … n-1","marks":[{"type":"code"}]},{"type":"text","text":") and one-way routes between them. A route "},{"type":"text","text":"[from, to, minutes]","marks":[{"type":"code"}]},{"type":"text","text":" means travelling from "},{"type":"text","text":"from","marks":[{"type":"code"}]},{"type":"text","text":" to "},{"type":"text","text":"to","marks":[{"type":"code"}]},{"type":"text","text":" takes "},{"type":"text","text":"minutes","marks":[{"type":"code"}]},{"type":"text","text":" minutes. The dispatcher wants to know how fast a parcel from warehouse "},{"type":"text","text":"source","marks":[{"type":"code"}]},{"type":"text","text":" can reach "},{"type":"text","text":"every","marks":[{"type":"bold"}]},{"type":"text","text":" hub."}]},{"type":"heading","attrs":{"level":2,"id":"task"},"content":[{"type":"text","text":"Task"}]},{"type":"paragraph","content":[{"type":"text","text":"Return an array of length "},{"type":"text","text":"n","marks":[{"type":"code"}]},{"type":"text","text":" whose element "},{"type":"text","text":"i","marks":[{"type":"code"}]},{"type":"text","text":" is the shortest total travel time from "},{"type":"text","text":"source","marks":[{"type":"code"}]},{"type":"text","text":" to hub "},{"type":"text","text":"i","marks":[{"type":"code"}]},{"type":"text","text":", or "},{"type":"text","text":"-1","marks":[{"type":"code"}]},{"type":"text","text":" when the hub is unreachable. The distance to "},{"type":"text","text":"source","marks":[{"type":"code"}]},{"type":"text","text":" is "},{"type":"text","text":"0","marks":[{"type":"code"}]},{"type":"text","text":"."}]},{"type":"bulletList","content":[{"type":"listItem","content":[{"type":"paragraph","content":[{"type":"text","text":"Routes are "},{"type":"text","text":"directed","marks":[{"type":"bold"}]},{"type":"text","text":", may repeat (the cheapest counts) and may lead from a hub back to itself."}]}]},{"type":"listItem","content":[{"type":"paragraph","content":[{"type":"text","text":"Times are non-negative ("},{"type":"text","text":"0","marks":[{"type":"code"}]},{"type":"text","text":" included)."}]}]},{"type":"listItem","content":[{"type":"paragraph","content":[{"type":"text","text":"Sums of times can exceed the 32-bit "},{"type":"text","text":"int","marks":[{"type":"code"}]},{"type":"text","text":" range – in C# and Java the result is "},{"type":"text","text":"long[]","marks":[{"type":"code"}]},{"type":"text","text":"."}]}]}]},{"type":"paragraph","content":[{"type":"text","text":"Signatures: "},{"type":"text","text":"shortestDistances(n, edges, source)","marks":[{"type":"code"}]},{"type":"text","text":" (JS/TS), "},{"type":"text","text":"shortest_distances(n, edges, source)","marks":[{"type":"code"}]},{"type":"text","text":" (Python), "},{"type":"text","text":"Solution.ShortestDistances","marks":[{"type":"code"}]},{"type":"text","text":" (C#, "},{"type":"text","text":"long[]","marks":[{"type":"code"}]},{"type":"text","text":"), "},{"type":"text","text":"Solution.shortestDistances","marks":[{"type":"code"}]},{"type":"text","text":" (Java, "},{"type":"text","text":"long[]","marks":[{"type":"code"}]},{"type":"text","text":"), "},{"type":"text","text":"ShortestDistances","marks":[{"type":"code"}]},{"type":"text","text":" (Go, "},{"type":"text","text":"[]int","marks":[{"type":"code"}]},{"type":"text","text":")."}]},{"type":"heading","attrs":{"level":2,"id":"examples"},"content":[{"type":"text","text":"Examples"}]},{"type":"table","content":[{"type":"tableRow","content":[{"type":"tableHeader","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"n","marks":[{"type":"code"}]}]}]},{"type":"tableHeader","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"edges","marks":[{"type":"code"}]}]}]},{"type":"tableHeader","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"source","marks":[{"type":"code"}]}]}]},{"type":"tableHeader","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"Result"}]}]}]},{"type":"tableRow","content":[{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"4","marks":[{"type":"code"}]}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"[[0,1,4], [0,2,1], [2,1,2], [1,3,1], [2,3,5]]","marks":[{"type":"code"}]}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"0","marks":[{"type":"code"}]}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"[0, 3, 1, 4]","marks":[{"type":"code"}]}]}]}]},{"type":"tableRow","content":[{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"4","marks":[{"type":"code"}]}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"[[0,3,10], [0,1,1], [1,2,1], [2,3,1]]","marks":[{"type":"code"}]}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"0","marks":[{"type":"code"}]}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"[0, 1, 2, 3]","marks":[{"type":"code"}]},{"type":"text","text":" – more hops, less time"}]}]}]},{"type":"tableRow","content":[{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"3","marks":[{"type":"code"}]}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"[[1,0,5], [0,2,7]]","marks":[{"type":"code"}]}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"0","marks":[{"type":"code"}]}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"[0, -1, 7]","marks":[{"type":"code"}]},{"type":"text","text":" – routes are one-way"}]}]}]}]},{"type":"heading","attrs":{"level":2,"id":"constraints"},"content":[{"type":"text","text":"Constraints"}]},{"type":"bulletList","content":[{"type":"listItem","content":[{"type":"paragraph","content":[{"type":"text","text":"1 ≤ n ≤ 100,000","marks":[{"type":"code"}]},{"type":"text","text":", up to 200,000 routes, "},{"type":"text","text":"0 ≤ minutes ≤ 10⁶","marks":[{"type":"code"}]},{"type":"text","text":"."}]}]}]},{"type":"heading","attrs":{"level":2,"id":"expected-complexity"},"content":[{"type":"text","text":"Expected complexity"}]},{"type":"paragraph","content":[{"type":"text","text":"O((V + E) log V)","marks":[{"type":"bold"}]},{"type":"text","text":" time, "},{"type":"text","text":"O(V + E)","marks":[{"type":"bold"}]},{"type":"text","text":" memory."}]}]},"hints":[],"hintsCount":3,"quiz":null,"solutionNotes":null,"unlocked":false,"external":[{"provider":"leetcode","url":"https://leetcode.com/problems/network-delay-time/","title":"LeetCode 743 – Network Delay Time","difficulty":null}],"articles":[],"topicsInfo":[],"author":{"id":"6b08bd2e80884893657cc8cf","name":"Redakcja Art of Devs","avatar":null,"handle":"redakcja","community":false},"capabilities":{"judge0":false,"aiReview":false},"createdAt":"2026-11-27T08:22:53.778Z","updatedAt":"2026-11-27T08:22:53.778Z"}