-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdijksta.cpp
More file actions
65 lines (54 loc) · 1.21 KB
/
dijksta.cpp
File metadata and controls
65 lines (54 loc) · 1.21 KB
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
62
63
64
65
#include<bits/stdc++.h>
using namespace std;
typedef pair<int, int> iPair;
# define INF 1e18
vector<pair<int,int> > adj[100005];
int steps[100005];
void shortestPath(int V, int src)
{
steps[1]=0;
steps[V-1]=-1;
priority_queue< iPair, vector <iPair> , greater<iPair> > pq;
vector<long long> dist(V, INF);
pq.push(make_pair(0, src));
dist[src] = 0;
while (!pq.empty())
{
int u = pq.top().second;
pq.pop();
for (auto x : adj[u])
{
int v = x.first;
int weight = x.second;
if (dist[v] > dist[u] + weight)
{
dist[v] = dist[u] + weight;
steps[v] = u;
pq.push(make_pair(dist[v], v));
}
}
}
int kid = V-1;
if(steps[V-1]==-1){
cout<<-1;
}else{
vector<int> ans;
while(kid!=0){
ans.push_back(kid);
kid=steps[kid];
}
for(int i=ans.size()-1;i>=0;i--)
cout<<ans[i]<<" ";
}
}
int main(){
int n,m;
cin>>n>>m;
for(int i=0;i<m;i++){
int a,b,c;
cin>>a>>b>>c;
adj[a].push_back({b,c});
adj[b].push_back({a,c});
}
shortestPath(n+1,1);
}