/
boj_11779.cpp
85 lines (68 loc) · 1.26 KB
/
boj_11779.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
81
82
83
84
85
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
vector<pii> graph[1001];
int dist[1001];
int parent[1001];
int n;
int m;
int s, e;
void Dijkstra(int start)
{
for (int i = 1; i <= n; i++)
dist[i] = INT_MAX;
dist[start] = 0;
parent[start] = -1;
priority_queue<pii, vector<pii>, greater<>> pq;
pq.push({ dist[start], start });
while (!pq.empty())
{
int cur_node = pq.top().second;
int cur_weight = pq.top().first;
pq.pop();
if (cur_weight > dist[cur_node])
continue;
for (auto& e : graph[cur_node])
{
int next_node = e.first;
int next_weight = cur_weight + e.second;
if (next_weight < dist[next_node])
{
parent[next_node] = cur_node;
pq.push({ next_weight, next_node });
dist[next_node] = next_weight;
}
}
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
cin >> n;
cin >> m;
for (int i = 0; i < m; i++)
{
int a, b, c;
cin >> a >> b >> c;
graph[a].push_back({ b, c });
}
cin >> s >> e;
Dijkstra(s);
cout << dist[e] << "\n";
stack<int> sta;
int index = e;
sta.push(index);
do
{
index = parent[index];
sta.push(index);
} while (parent[index] != -1);
cout << sta.size() << "\n";
while (!sta.empty())
{
cout << sta.top() << " ";
sta.pop();
}
return 0;
}