-
Notifications
You must be signed in to change notification settings - Fork 13
/
dijkastra1.cpp
61 lines (43 loc) · 1.07 KB
/
dijkastra1.cpp
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include<bits/stdc++.h>
using namespace std;
void dijkstra(vector<pair<int,int>> adj[], int src, int n){
vector<int> dist(n, INT_MAX);
dist[src]=0;
priority_queue<pair<int, int>> pq;
pq.push({0,src});
while (!pq.empty())
{
int mdist=pq.top().first;
int cur_node=pq.top().second;
pq.pop();
for(pair<int,int> nbr: adj[cur_node]){
int weight=nbr.second;
int nbrP=nbr.first;
if(mdist+weight<dist[nbrP]){
dist[nbrP]=mdist+weight;
pq.push({dist[nbrP],nbrP});
}
}
}
for(int i=0;i<n;i++){
cout<<"shortest path from "<<src<<" to "<<i<< " is "<<dist[i]<<"\n";
}
}
int main()
{
int v;
int e;
cout<<"enter no. of vertex and edges";
cin>>v>>e;
vector<pair<int,int>> adj[v];
for(int i=0;i<e;i++){
int u,v,w;
cin>>u>>v>>w;
adj[u].push_back({v,w});
adj[v].push_back({u,w});
}
cout<<"plz enter the source node";
int src;
cin>>src;
dijkstra(adj,src,v);
}