-
Notifications
You must be signed in to change notification settings - Fork 0
/
BOJ_13913번_2.cpp
92 lines (64 loc) · 1.37 KB
/
BOJ_13913번_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
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
//다익스트라로 풀이
#include<iostream>
#include<queue>
#include<vector>
#include<functional>
using namespace std;
#define MAX_SIZE 100001
#define INF 987654321
typedef pair<int, int> P;
int n, k;
vector<int> vt;
priority_queue<P, vector<P>, greater<P>> pq;
int dist[MAX_SIZE];
int parent[MAX_SIZE];
void dikstra(int start) {
dist[start] = 0;
pq.push(P(dist[start], start));
while (!pq.empty()) {
int now = pq.top().second;
pq.pop();
if (now == k) return;
// X - 1
int next = now - 1;
if (0 <= next) {
if (dist[next] > dist[now] + 1) {
dist[next] = dist[now] + 1;
pq.push(P(dist[next], next));
parent[next] = now;
}
}
// X + 1
next = now + 1;
if (next <= 100000) {
if (dist[next] > dist[now] + 1) {
dist[next] = dist[now] + 1;
pq.push(P(dist[next], next));
parent[next] = now;
}
}
// 2*X
next = now * 2;
if (next <= 100000) {
if (dist[next] > dist[now] + 1) {
dist[next] = dist[now] + 1;
pq.push(P(dist[next], next));
parent[next] = now;
}
}
}
}
int main(void) {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> k;
for (int i = 0; i <= 100000; i++) dist[i] = INF;
dikstra(n);
for (int i = k; i != n; i = parent[i]) {
vt.push_back(i);
}
vt.push_back(n);
cout << vt.size() - 1 << '\n';
for (int i = vt.size() - 1; 0 <= i; i--) cout << vt[i] << " ";
return 0;
}