-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShortest Path in Weighted undirected graph 13-07-24 POTD.cpp
48 lines (45 loc) · 1.4 KB
/
Shortest Path in Weighted undirected graph 13-07-24 POTD.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
// https://www.geeksforgeeks.org/problems/shortest-path-in-weighted-undirected-graph/1
class Solution {
public:
vector<int> shortestPath(int n, int m, vector<vector<int>>& edges) {
vector<pair<int,int>>adj[n+1];
for(auto it:edges){
adj[it[0]].push_back({it[1],it[2]});
adj[it[1]].push_back({it[0],it[2]});
}
priority_queue<pair<int,int>,vector<pair<int,int>>,
greater<pair<int,int>>>pq;
pq.push({0,1});
vector<int>dis(n+1,INT_MAX),par(n+1);
for(int i=1; i<n+1;i++){
par[i]=i;
}
dis[1]=0;
while(!pq.empty()){
auto it=pq.top();
int node=it.second;
int dist=it.first;
pq.pop();
for(auto it:adj[node]){
int adjnode=it.first;
int weight=it.second;
if(dist+weight<dis[adjnode]){
dis[adjnode]=dist+weight;
pq.push({dist+weight,adjnode});
par[adjnode]=node;
}
}
}
if(dis[n]==INT_MAX) return {-1};
vector<int >path;
int node=n;
while(par[node]!=node){
path.push_back(node);
node=par[node];
}
path.push_back(1);
path.push_back(dis[n]);
reverse(path.begin(),path.end());
return path;
}
};