-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path20c.cpp
80 lines (75 loc) · 1.79 KB
/
20c.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
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using p = pair<ll, ll>;
const ll INF = 100'000'000'000'000;
inline void quick_IO(){
ios_base :: sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
}
vector<p> adj[100005];
bool visited[100005];
ll dist[100005];
priority_queue<p, vector<p>, greater<p>> pq;
int prevIndex[100005];
stack<int> answer;
int main(){
quick_IO();
int n, m;
int a, b, w;
cin>>n>>m;
for (int i = 0; i < m; ++i) {
cin>>a>>b>>w;
adj[a].emplace_back(b, w);
adj[b].emplace_back(a, w);
}
fill(prevIndex+1, prevIndex+n+1, -1);
pq.push({0, 1});
fill(dist+1, dist+n+1, INF);
dist[1] = 0;
while(!pq.empty()){
ll curr;
do{
curr = pq.top().second;
pq.pop();
}while(!pq.empty()&&visited[curr]);
if(visited[curr]){
break;
}
visited[curr] = true;
for(auto x:adj[curr]){
auto [nextIndex, nextWeight] = x;
if(dist[nextIndex]>nextWeight+dist[curr]){
prevIndex[nextIndex] = curr;
dist[nextIndex] = nextWeight+dist[curr];
pq.push({dist[nextIndex], nextIndex});
}
}
}
int searchIndex = n;
while(searchIndex!=1){
answer.push(searchIndex);
if(searchIndex==-1){
cout<<-1<<"\n";
return 0;
}
// cout<<prevIndex[searchIndex]<<endl;
searchIndex = prevIndex[searchIndex];
}
cout<<"1 ";
while(!answer.empty()){
cout<<answer.top()<<" ";
answer.pop();
}
cout<<"\n";
return 0;
}