-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path13549.cpp
45 lines (35 loc) · 968 Bytes
/
13549.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
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#define MAX_SIZE 100000+1
using namespace std;
int N, K;
bool visited[MAX_SIZE];
int dijkstra(){
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
pq.push({0, N});
while (!pq.empty()){
int cost = pq.top().first;
int cur = pq.top().second;
pq.pop();
if(cur == K) return cost;
if(cur * 2 < MAX_SIZE && !visited[cur * 2]){
pq.push({cost, cur * 2});
visited[cur * 2] = true;
}
if(cur + 1 < MAX_SIZE && !visited[cur + 1]){
pq.push({cost + 1, cur + 1});
visited[cur + 1] = true;
}
if(cur - 1 >= 0 && !visited[cur - 1]){
pq.push({cost + 1, cur - 1});
visited[cur - 1] = true;
}
}
}
int main(){
ios::sync_with_stdio(false); cin.tie(NULL);
cin >> N >> K;
cout << dijkstra();
}