forked from Master-Helix/DSA-Graphs_Important_Codes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDIJKSTRA_ALGO.cpp
More file actions
33 lines (31 loc) · 776 Bytes
/
Copy pathDIJKSTRA_ALGO.cpp
File metadata and controls
33 lines (31 loc) · 776 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// DIJKSTRA ALGO for Shortest Path between two vertex for UNDIRECTED Graph with different Weights
void DIJKSTRA(vector<pair<int,int>>adj[],int v,int src)
{
priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>pq; // min-heap
vector<int>dist(v+1,INT_MAX);
dist[src]=0;
pq.push({0,src});
while(!pq.empty())
{
auto k=pq.top();
int distance=k.first;
int node=k.second;
pq.pop();
for(auto it:adj[node])
{
int n1=it.first;
int n2=it.second;
if(dist[n1]>dist[node]+n2)
{
dist[n1]=dist[node]+n2;
pq.push({dist[n1],n1});
}
}
}
cout<<endl<<"DIJKSTRA Algo"<<endl;
cout<<"The distances from source are :"<<endl;
for(int i=0;i<=v;i++)
{
cout<<src<<"--->"<<i<<"=="<<dist[i]<<endl;
}
}