From 5a97c1d6c7eef1e4631ef66c5bc5a948f09de2b9 Mon Sep 17 00:00:00 2001 From: photosoftrips <115392419+photosoftrips@users.noreply.github.com> Date: Sun, 9 Oct 2022 12:46:16 +0530 Subject: [PATCH] Create dijkstra.cpp --- dijkstra.cpp | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 dijkstra.cpp diff --git a/dijkstra.cpp b/dijkstra.cpp new file mode 100644 index 0000000..4762717 --- /dev/null +++ b/dijkstra.cpp @@ -0,0 +1,27 @@ + vector dijkstra(int V, vector> adj[], int S) + { + // Code here + priority_queue,vector>, greater>> minh ; + vector dis(V,INT_MAX-1) ; + dis[S] = 0 ; + minh.push({0,S}) ; + while(!minh.empty()) + { + auto it = minh.top() ; + minh.pop() ; + int a = it.first ; + int b = it.second ; + + for(auto i : adj[b]) + { + int m = i[0] ; + int n = i[1] ; + if(dis[m] > a + n ){ + dis[m] = a+n ; + minh.push({dis[m],m}) ; + } + } + } + + return dis ; + }