-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11779.cpp
94 lines (77 loc) · 1.76 KB
/
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
86
87
88
89
90
91
92
93
94
#include <iostream>
#include <vector>
#include <queue>
#define INF 1e9
#define pii pair<int,int>
using namespace std;
vector<bool> visits;
vector<int> dist;
vector<vector<pii>> edge;
vector<int> prevs;
// MinHeap이므로 오름차순 정렬
struct comp{
bool operator()(pii b, pii a)
{
return a.second < b.second;
}
};
priority_queue<pii, vector<pii>, comp> pq;
int n, m;
void dijkstra()
{
while(!pq.empty())
{
int node, d;
node = pq.top().first;
d = pq.top().second;
pq.pop();
if(visits[node] == true) continue;
visits[node] = true;
for (auto& e:edge[node])
{
int next, nextd;
next = e.first;
nextd = e.second;
if (dist[node] + nextd < dist[next])
{
// Relxation
dist[next] = dist[node] + nextd;
prevs[next] = node;
pq.push({next, dist[next]});
}
}
}
}
int main()
{
cin >> n >> m;
// 초기화
visits = vector<bool> (1001, false);
dist = vector<int>(1001, INF);
prevs = vector<int>(1001, -1);
edge = vector<vector<pii>> (1001, vector<pii> {});
int start, end, d;
for (int i = 0; i < m; i++)
{
cin >> start >> end >> d;
edge[start].push_back({end, d});
}
cin >> start >> end;
pq.push({start, 0});
dist[start] = 0;
dijkstra();
cout << dist[end] << endl;
int cnt = 0;
vector<int> answer;
while (end != start)
{
answer.push_back(end);
end = prevs[end];
cnt++;
}
cnt++;
answer.push_back(end);
cout << cnt << endl;
for (auto iter = answer.rbegin(); iter!= answer.rend(); iter++)
cout << *iter << " ";
}