-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path210315_BOJ_11779.cpp
More file actions
75 lines (60 loc) · 1.14 KB
/
Copy path210315_BOJ_11779.cpp
File metadata and controls
75 lines (60 loc) · 1.14 KB
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
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
const int INF = 987654321;
int N, M, f, t;
vector<pair<int, int>> E[1001];
int D[1001];
priority_queue<pair<int, int>> pq;
int visit[1001]{ 0 };
void input() {
cin >> N;
cin >> M;
for (int i = 0; i < M; ++i) {
int a, b, c; cin >> a >> b >> c;
E[a].push_back({ c, b });
}
cin >> f >> t;
}
void Dijkstra() {
for (int i = 1; i <= N; ++i)
D[i] = INF;
D[f] = 0;
pq.push({ 0, f });
while (!pq.empty()) {
int x = pq.top().second;
int cost = pq.top().first * -1;
pq.pop();
for (int i = 0; i < E[x].size(); ++i) {
int y = E[x][i].second;
int sum = cost + E[x][i].first;
if (D[y] > sum) {
D[y] = sum;
pq.push({ -sum, y });
visit[y] = x;
}
}
}
cout << D[t] << "\n";
int index = t;
vector<int> res;
res.push_back(t);
while (1) {
res.push_back(visit[index]);
index = visit[index];
if (visit[index] == 0)
break;
}
cout << res.size() << "\n";
for (int i = res.size() - 1; i >= 0; --i)
cout << res[i] << " ";
cout << "\n";
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
input();
Dijkstra();
return 0;
}