-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path20C-Dijsktra.cpp
61 lines (59 loc) · 1.37 KB
/
20C-Dijsktra.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
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <set>
#include <queue>
#define N 100010
#define REP(i,a,n) for(int i=a;i<=n;i++)
using namespace std;
int n, m, i, j;
vector<long long>D(N, 1e18);
vector<int>P(N, -1);
bool visited[N];
vector<pair<int, int>>G[N];
priority_queue<pair<int, int>> Q;
int main(){
cin >> n >> m;
//.memset(visited, 0, sizeof(visited));
vector<long long>T;
long long X, Y, Z;
REP(i, 1, m){
cin >> X >> Y >> Z;
G[X].push_back(make_pair(Y, Z));
G[Y].push_back(make_pair(X, Z));
}
D[1] = 0;
Q.push(make_pair(-D[1], 1));
while (!Q.empty()){
pair<int, int> a = Q.top();
Q.pop();
int v = a.second;
long long d = -a.first;
if (D[v] < d)continue;
long long to;
for (int j = 0; j < G[v].size(); j++){
to = G[v][j].first;
int length = G[v][j].second;
if (D[to]>length + D[v]){
P[to] = v;
D[to] = length + D[v];
Q.push(make_pair(-D[to], to));
}
}
}
if (D[n] == 1e18){
cout << "-1";
return 0;
}
while (n != -1){
T.push_back(n);
n = P[n];
}
reverse(T.begin(), T.end());
for (long long path : T){
cout << path << " ";
}
//system("pause");
return 0;
}