{"id":"6b093dddbd4755deb67ee064","slug":"dijkstra-shortest-paths","title":"Najkrótsze czasy dostaw (Dijkstra)","summary":"Sieć hubów logistycznych z jednokierunkowymi trasami o różnych czasach przejazdu. Liczymy najkrótszy czas dojazdu z magazynu do każdego huba – algorytm Dijkstry z kopcem, który w części języków trzeba napisać samemu.","servedLang":"pl","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":"pl","statement":{"type":"doc","content":[{"type":"paragraph","content":[{"type":"text","text":"Firma kurierska ma "},{"type":"text","text":"n","marks":[{"type":"code"}]},{"type":"text","text":" hubów (numery "},{"type":"text","text":"0 … n-1","marks":[{"type":"code"}]},{"type":"text","text":") i jednokierunkowe trasy między nimi. Trasa "},{"type":"text","text":"[from, to, minutes]","marks":[{"type":"code"}]},{"type":"text","text":" oznacza przejazd z "},{"type":"text","text":"from","marks":[{"type":"code"}]},{"type":"text","text":" do "},{"type":"text","text":"to","marks":[{"type":"code"}]},{"type":"text","text":" w "},{"type":"text","text":"minutes","marks":[{"type":"code"}]},{"type":"text","text":" minut. Dyspozytor chce wiedzieć, jak szybko paczka z magazynu "},{"type":"text","text":"source","marks":[{"type":"code"}]},{"type":"text","text":" może dotrzeć do "},{"type":"text","text":"każdego","marks":[{"type":"bold"}]},{"type":"text","text":" huba."}]},{"type":"heading","attrs":{"level":2,"id":"zadanie"},"content":[{"type":"text","text":"Zadanie"}]},{"type":"paragraph","content":[{"type":"text","text":"Funkcja zwraca tablicę długości "},{"type":"text","text":"n","marks":[{"type":"code"}]},{"type":"text","text":", w której element "},{"type":"text","text":"i","marks":[{"type":"code"}]},{"type":"text","text":" to najkrótszy łączny czas przejazdu z "},{"type":"text","text":"source","marks":[{"type":"code"}]},{"type":"text","text":" do huba "},{"type":"text","text":"i","marks":[{"type":"code"}]},{"type":"text","text":", albo "},{"type":"text","text":"-1","marks":[{"type":"code"}]},{"type":"text","text":", gdy hub jest nieosiągalny. Odległość do "},{"type":"text","text":"source","marks":[{"type":"code"}]},{"type":"text","text":" wynosi "},{"type":"text","text":"0","marks":[{"type":"code"}]},{"type":"text","text":"."}]},{"type":"bulletList","content":[{"type":"listItem","content":[{"type":"paragraph","content":[{"type":"text","text":"Trasy są "},{"type":"text","text":"skierowane","marks":[{"type":"bold"}]},{"type":"text","text":", mogą się powtarzać (liczy się najtańsza), mogą prowadzić z huba do niego samego."}]}]},{"type":"listItem","content":[{"type":"paragraph","content":[{"type":"text","text":"Czasy są nieujemne (także "},{"type":"text","text":"0","marks":[{"type":"code"}]},{"type":"text","text":")."}]}]},{"type":"listItem","content":[{"type":"paragraph","content":[{"type":"text","text":"Sumy czasów mogą przekroczyć zakres 32-bitowego "},{"type":"text","text":"int","marks":[{"type":"code"}]},{"type":"text","text":" – w C# i Javie wynik to "},{"type":"text","text":"long[]","marks":[{"type":"code"}]},{"type":"text","text":"."}]}]}]},{"type":"paragraph","content":[{"type":"text","text":"Sygnatury: "},{"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":"przyklady"},"content":[{"type":"text","text":"Przykłady"}]},{"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":"Wynik"}]}]}]},{"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":" – więcej przesiadek, krótszy czas"}]}]}]},{"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":" – trasy są jednokierunkowe"}]}]}]}]},{"type":"heading","attrs":{"level":2,"id":"ograniczenia"},"content":[{"type":"text","text":"Ograniczenia"}]},{"type":"bulletList","content":[{"type":"listItem","content":[{"type":"paragraph","content":[{"type":"text","text":"1 ≤ n ≤ 100 000","marks":[{"type":"code"}]},{"type":"text","text":", do 200 000 tras, "},{"type":"text","text":"0 ≤ minutes ≤ 10⁶","marks":[{"type":"code"}]},{"type":"text","text":"."}]}]}]},{"type":"heading","attrs":{"level":2,"id":"oczekiwana-zlozonosc"},"content":[{"type":"text","text":"Oczekiwana złożoność"}]},{"type":"paragraph","content":[{"type":"text","text":"Czas "},{"type":"text","text":"O((V + E) log V)","marks":[{"type":"bold"}]},{"type":"text","text":", pamięć "},{"type":"text","text":"O(V + E)","marks":[{"type":"bold"}]},{"type":"text","text":"."}]}]},"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"}