forked from gkushalg01/cp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dijkstra.cpp
89 lines (74 loc) · 1.6 KB
/
dijkstra.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// Dijkstra's Algorithm to find shortest path between a source and a destination vertex
// and also to print the path followed
#include<bits/stdc++.h>
using namespace std;
#define int long long int
#define w(x) int x; cin>>x; while(x--)
#define print(arr) for(auto x:arr) cout<<x<<" ";
#define vi vector<int>
#define pb push_back
#define MAX 1e18
#define vvpi vector<vector<pair<int,int>>>
void print_parent(vi &parent,int dst)
{
vi ans;
while(dst != -1)
{
ans.pb(dst);
dst = parent[dst];
}
reverse(ans.begin(),ans.end());
print(ans);
}
void apply_dijkstra(vvpi &gr, vi &dist, vi &parent,int src,int dst)
{
set<pair<int,int>> weight;
pair<int,int> pr;
int temp,node,i,curr_nd,curr_dis;
weight.insert({0,src});
dist[src] = 0;
parent[src] = -1;
while(!weight.empty())
{
pr = *(weight.begin());
temp = pr.first;
node = pr.second;
weight.erase(weight.begin());
for(i=0;i<gr[node].size();i++)
{
curr_nd = gr[node][i].first;
curr_dis = gr[node][i].second;
if(dist[curr_nd]>dist[node]+curr_dis)
{
auto q = weight.find({dist[curr_nd],curr_nd});
if(q != weight.end()){
weight.erase(q);
}
dist[curr_nd] = dist[node]+curr_dis;
parent[curr_nd] = node;
weight.insert({dist[curr_nd],curr_nd});
}
}
}
}
int32_t main()
{
int n,m,i,x,y,node,len,temp;
cin>>n>>m;
vvpi gr(n+1);
set<pair<int,int>> weight;
pair<int,int> pr;
vi dist(n+1,MAX),parent(n+1);
for(i=0;i<m;i++)
{
cin>>x>>y>>len;
gr[x].pb({y,len});
gr[y].pb({x,len});
}
apply_dijkstra(gr,dist,parent,1,n);
if(dist[n] != MAX)
print_parent(parent,n);
else
cout<<"-1";
return 0;
}