-
Notifications
You must be signed in to change notification settings - Fork 0
/
dijkstra_graph.cpp
69 lines (62 loc) · 1.25 KB
/
dijkstra_graph.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
#include<bits/stdc++.h>
#define ll long long int
#define vi vector<int>
#define vll vector<ll>
#define vvi vector<vi>
#define vvl vector<vll>
#define pb push_back
#define mp make_pair
#define pii pair<int,int>
#define pll pair<ll,ll>
#define vpii vector<pii >
#define vpll vector<pll >
#define ff first
#define ss second
#define fastio ios_base::sync_with_stdio(false) , cin.tie(NULL) ,cout.tie(NULL)
using namespace std;
const int INF = 1e9;
vpii g[100010];
vi p(100002,-1),d(100002,INF);
priority_queue<pii ,vpii ,greater<pii>> q;
void dijkstra(int s){
d[s]=0;
q.push({0,s});
while(!q.empty()){
int v=q.top().ss;
int len=q.top().ff;
q.pop();
if(len!=d[v])
continue;
for(pii x: g[v]){
if(d[v]+x.ss<d[x.ff])
{
d[x.ff]=x.ss+d[v];
p[x.ff]=v;
q.push({d[x.ff],x.ff});
}
}
}
}
vector<int> restore_path(int s, int t) {
vector<int> path;
for (int v = t; v != s; v = p[v])
path.push_back(v);
path.push_back(s);
reverse(path.begin(), path.end());
return path;
}
int main(){
fastio;
int n,m;
cin>>n>>m;
while(m--){
int u,v,l;
cin>>u>>v>>l;
g[u].pb({v,l});
g[v].pb({u,l});
}
dijkstra(1);
vi a=restore_path(1,n);
for(int i:a ) cout<<i<<" ";
return 0;
}