forked from hughbzhang/Competitive_Programming
/
dijk.cpp
72 lines (64 loc) · 1.54 KB
/
dijk.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
#include <cstdio>
#include <iostream>
#include <queue>
#include <utility>
#include <vector>
#include <algorithm>
#define A first
#define B second
#define MAX 200000
#define LARGE (1e15)
using namespace std;
typedef long long ll;
typedef pair<ll,int> pii;
typedef vector<pii>::iterator vi;
int vert,edges;
vector<pii> list[MAX];
bool vis[MAX];
int pre[MAX];
ll dist[MAX];
priority_queue<pii,vector<pii>, greater<pii> > q;
int ans[MAX];
void search(){
for(int x = 0;x<MAX;x++) dist[x] = LARGE;
dist[1] = 0;
q.push(pii(0,1));
while(!q.empty()){
pii cur = q.top();
q.pop();
if(vis[cur.B]) continue;
vis[cur.B] = true;
for(vi it = list[cur.B].begin();it!=list[cur.B].end();++it){
if(dist[it->B]>dist[cur.B]+it->A){
pre[it->B] = cur.B;
dist[it->B] = dist[cur.B]+it->A;
q.push(pii(dist[it->B],it->B));
}
}
}
}
int main(){
freopen("dijkstra.in","r",stdin);
freopen("dijkstra.out","w",stdout);
cin >> vert >> edges;
int a,b,c;
for(int x = 0;x<edges;x++){
cin >> a >> b >> c;
list[a].push_back(pii(c,b));
list[b].push_back(pii(c,a));
}
search();
int cnt = 0;
int now = vert;
// for(int x = 0;x<=vert;x++) cout << dist[x] << endl;
if(dist[vert]==LARGE) cout << -1 << endl;
else{
while(now!=0){
ans[cnt] = now;
cnt++;
now = pre[now];
}
}
for(int x = cnt-1;x>=0;x--) cout << ans[x] << " ";
return 0;
}