From b53f274a0a309aac5f719ce1a7556147bcd08243 Mon Sep 17 00:00:00 2001 From: Bhanuka Rathnayaka Date: Tue, 22 Oct 2019 11:51:48 +0530 Subject: [PATCH] Solution for Hackerrank Dijkstra Shortest Reach 2 --- .../Graph Theory/DijkstraShortestReach2.py | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 Algorithms/Graph Theory/DijkstraShortestReach2.py diff --git a/Algorithms/Graph Theory/DijkstraShortestReach2.py b/Algorithms/Graph Theory/DijkstraShortestReach2.py new file mode 100644 index 0000000..8a16a1c --- /dev/null +++ b/Algorithms/Graph Theory/DijkstraShortestReach2.py @@ -0,0 +1,49 @@ + +from collections import defaultdict + +import heapq + + +def dijkstra(S, N, G): + D = {} + H = [(0, S)] + + for n in range(1, N + 1): + D[n] = float('inf') + + D[S] = 0 + + while H: + t = heapq.heappop(H) + for h in G[t[1]]: + if D[h[0]] > D[t[1]] + h[1]: + D[h[0]] = D[t[1]] + h[1] + if (h[1], h[0]) in H: + H.remove((h[1], h[0])) + heapq.heapify(H) + heapq.heappush(H, (D[h[0]], h[0])) + + return D + + +def main(): + T = int(input()) + + for _ in range(T): + N, M = [int(i) for i in input().split()] + + G = defaultdict(set) + + for _ in range(M): + e = [int(i) for i in input().split()] + G[e[0]].add((e[1], e[2])) + G[e[1]].add((e[0], e[2])) + + S = int(input()) + + D = dijkstra(S, N, G) + print(' '.join(str(D[n]) if D[n] != float('inf') else '-1' for n in range(1, N + 1) if n != S)) + + +if __name__ == "__main__": + main()