-
Notifications
You must be signed in to change notification settings - Fork 0
/
11779번 최소비용 구하기 2.cpp
45 lines (42 loc) · 1.09 KB
/
11779번 최소비용 구하기 2.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
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <unordered_map>
using namespace std;
#define INF 100000001
int main() {
int n, m, a, b, c;
scanf("%d%d", &n, &m);
vector<unordered_map<int, int>> cost(n+1, unordered_map<int,int>());
for (int i = 0; i < m; i++) {
scanf("%d%d%d", &a, &b, &c);
if (cost[a][b] == 0 || cost[a][b] > c) cost[a][b] = c;
}
scanf("%d%d", &a, &b);
priority_queue<pair<int, int>> pq;
vector<pair<int, int>> dist(n + 1, { INF, 0 });
pair<int, int> cur;
dist[a] = { 0,-1 };
pq.push({ 0, a });
while (!pq.empty()) {
cur = pq.top();
pq.pop();
for (auto i : cost[cur.second]) {
if (dist[i.first].first > cur.first + i.second) {
dist[i.first].first = cur.first + i.second;
dist[i.first].second = cur.second;
pq.push({ dist[i.first].first, i.first });
}
}
}
printf("%d\n", dist[b].first);
vector<int> answer;
while (b != -1) {
answer.push_back(b);
b = dist[b].second;
}
printf("%d\n", answer.size());
for (int i = answer.size() - 1; i >= 0; i--) printf("%d ", answer[i]);
}