#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i)
#define FORD(i, a, b) for (int i = (a), _b = (b); i >= _b ; --i)
#define REP(i, a) for (int i = 0, _a = (a); i < _a; ++i)
#define pp pair<int, int>
#define bit(S, i) (((S) >> i) & 1)
#define next __next
#define prev __prev
#define N 30030
#define sqrtN 200
#define taskname ""
using namespace std;
int n, m, b[N], p[N];
struct data {
int u, jump;
long long d;
};
struct cmp{
bool operator() (const data &a, const data &b) {
return a.d > b.d;
}
};
priority_queue<data, vector<data>, cmp> heap;
long long d[N][sqrtN];
vector<int> smallJump[N], largeJump[N];//, smallJumpVer[N];
void Dijkstra(int start, int finish) {
REP(i, n) REP(j, (int)sqrt(n) + 1) d[i][j] = 1e18;
d[start][0] = 0;
heap.push(data({start, 0, 0}));
while (!heap.empty()) {
data u = heap.top();
heap.pop();
if (u.u == finish) {
cout << u.d;
return;
}
if (d[u.u][u.jump] != u.d) continue;
if (u.jump == 0) {
// Change value of jump
for (int jump : smallJump[u.u]) {
if (d[u.u][jump] > d[u.u][0]) {
d[u.u][jump] = d[u.u][0];
heap.push(data({u.u, jump, d[u.u][0]}));
}
}
// Large jump ( <= sqrt(n) times because jump > sqrt(n) )
for (int jump : largeJump[u.u]) {
int newU = u.u - jump, c = 0;
while (newU >= 0) {
++c;
if (d[newU][0] > d[u.u][0] + c) {
d[newU][0] = d[u.u][0] + c;
heap.push(data({newU, 0, d[newU][0]}));
}
newU -= jump;
}
newU = u.u + jump, c = 0;
while (newU < n) {
++c;
if (d[newU][0] > d[u.u][0] + c) {
d[newU][0] = d[u.u][0] + c;
heap.push(data({newU, 0, d[newU][0]}));
}
newU += jump;
}
}
}
else {
// Jump left - right
int newU = u.u - u.jump;
if (newU >= 0 && d[newU][u.jump] > d[u.u][u.jump] + 1) {
d[newU][u.jump] = d[u.u][u.jump] + 1;
heap.push(data({newU, u.jump, d[newU][u.jump]}));
}
newU = u.u + u.jump;
if (newU < n && d[newU][u.jump] > d[u.u][u.jump] + 1) {
d[newU][u.jump] = d[u.u][u.jump] + 1;
heap.push(data({newU, u.jump, d[newU][u.jump]}));
}
// Change Jump to 0
if (d[u.u][0] > d[u.u][u.jump]) {
d[u.u][0] = d[u.u][u.jump];
heap.push(data({u.u, 0, d[u.u][0]}));
}
}
}
cout << -1;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);cout.tie(NULL);
cin >> n >> m;
int finish, start;
FOR(i, 1, m) {
int p, b;
cin >> b >> p;
if (i == 1) start = b;
if (i == 2) finish = b;
if (p <= (int) sqrt(n)) smallJump[b].push_back(p);
else largeJump[b].push_back(p);
}
Dijkstra(start, finish);
}
Compilation message
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:109:28: warning: 'start' may be used uninitialized in this function [-Wmaybe-uninitialized]
Dijkstra(start, finish);
^
skyscraper.cpp:109:28: warning: 'finish' may be used uninitialized in this function [-Wmaybe-uninitialized]
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
50760 KB |
Output is correct |
2 |
Correct |
1 ms |
50760 KB |
Output is correct |
3 |
Correct |
1 ms |
50760 KB |
Output is correct |
4 |
Correct |
1 ms |
50760 KB |
Output is correct |
5 |
Correct |
1 ms |
50760 KB |
Output is correct |
6 |
Correct |
0 ms |
50760 KB |
Output is correct |
7 |
Correct |
1 ms |
50760 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
50760 KB |
Output is correct |
2 |
Correct |
1 ms |
50760 KB |
Output is correct |
3 |
Correct |
0 ms |
50760 KB |
Output is correct |
4 |
Correct |
1 ms |
50760 KB |
Output is correct |
5 |
Correct |
1 ms |
50760 KB |
Output is correct |
6 |
Correct |
0 ms |
50760 KB |
Output is correct |
7 |
Correct |
1 ms |
50760 KB |
Output is correct |
8 |
Correct |
1 ms |
50760 KB |
Output is correct |
9 |
Correct |
1 ms |
50760 KB |
Output is correct |
10 |
Correct |
1 ms |
50760 KB |
Output is correct |
11 |
Correct |
2 ms |
50760 KB |
Output is correct |
12 |
Correct |
2 ms |
50760 KB |
Output is correct |
13 |
Correct |
1 ms |
50760 KB |
Output is correct |
14 |
Correct |
2 ms |
50760 KB |
Output is correct |
15 |
Correct |
2 ms |
50760 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
50760 KB |
Output is correct |
2 |
Correct |
1 ms |
50760 KB |
Output is correct |
3 |
Correct |
0 ms |
50760 KB |
Output is correct |
4 |
Correct |
1 ms |
50760 KB |
Output is correct |
5 |
Correct |
1 ms |
50760 KB |
Output is correct |
6 |
Correct |
0 ms |
50760 KB |
Output is correct |
7 |
Correct |
1 ms |
50760 KB |
Output is correct |
8 |
Correct |
0 ms |
50760 KB |
Output is correct |
9 |
Correct |
1 ms |
50760 KB |
Output is correct |
10 |
Correct |
0 ms |
50760 KB |
Output is correct |
11 |
Correct |
0 ms |
50760 KB |
Output is correct |
12 |
Correct |
2 ms |
50760 KB |
Output is correct |
13 |
Correct |
2 ms |
50760 KB |
Output is correct |
14 |
Correct |
2 ms |
50760 KB |
Output is correct |
15 |
Correct |
3 ms |
50760 KB |
Output is correct |
16 |
Correct |
2 ms |
50760 KB |
Output is correct |
17 |
Correct |
3 ms |
50760 KB |
Output is correct |
18 |
Correct |
3 ms |
50760 KB |
Output is correct |
19 |
Correct |
3 ms |
50760 KB |
Output is correct |
20 |
Correct |
0 ms |
50760 KB |
Output is correct |
21 |
Correct |
2 ms |
50760 KB |
Output is correct |
22 |
Correct |
0 ms |
50760 KB |
Output is correct |
23 |
Correct |
4 ms |
50760 KB |
Output is correct |
24 |
Correct |
4 ms |
50760 KB |
Output is correct |
25 |
Correct |
2 ms |
50760 KB |
Output is correct |
26 |
Correct |
4 ms |
50760 KB |
Output is correct |
27 |
Correct |
4 ms |
50760 KB |
Output is correct |
28 |
Correct |
4 ms |
50760 KB |
Output is correct |
29 |
Correct |
5 ms |
50760 KB |
Output is correct |
30 |
Correct |
2 ms |
50760 KB |
Output is correct |
31 |
Correct |
3 ms |
50760 KB |
Output is correct |
32 |
Correct |
2 ms |
50928 KB |
Output is correct |
33 |
Correct |
7 ms |
50940 KB |
Output is correct |
34 |
Correct |
4 ms |
50940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
50760 KB |
Output is correct |
2 |
Correct |
1 ms |
50760 KB |
Output is correct |
3 |
Correct |
0 ms |
50760 KB |
Output is correct |
4 |
Correct |
2 ms |
50760 KB |
Output is correct |
5 |
Correct |
0 ms |
50760 KB |
Output is correct |
6 |
Correct |
1 ms |
50760 KB |
Output is correct |
7 |
Correct |
1 ms |
50760 KB |
Output is correct |
8 |
Correct |
0 ms |
50760 KB |
Output is correct |
9 |
Correct |
1 ms |
50760 KB |
Output is correct |
10 |
Correct |
0 ms |
50760 KB |
Output is correct |
11 |
Correct |
0 ms |
50760 KB |
Output is correct |
12 |
Correct |
0 ms |
50760 KB |
Output is correct |
13 |
Correct |
2 ms |
50760 KB |
Output is correct |
14 |
Correct |
0 ms |
50760 KB |
Output is correct |
15 |
Correct |
0 ms |
50760 KB |
Output is correct |
16 |
Correct |
0 ms |
50760 KB |
Output is correct |
17 |
Correct |
1 ms |
50760 KB |
Output is correct |
18 |
Correct |
0 ms |
50760 KB |
Output is correct |
19 |
Correct |
4 ms |
50760 KB |
Output is correct |
20 |
Correct |
0 ms |
50760 KB |
Output is correct |
21 |
Correct |
2 ms |
50760 KB |
Output is correct |
22 |
Correct |
0 ms |
50760 KB |
Output is correct |
23 |
Correct |
4 ms |
50760 KB |
Output is correct |
24 |
Correct |
2 ms |
50760 KB |
Output is correct |
25 |
Correct |
2 ms |
50760 KB |
Output is correct |
26 |
Correct |
0 ms |
50760 KB |
Output is correct |
27 |
Correct |
2 ms |
50760 KB |
Output is correct |
28 |
Correct |
5 ms |
50760 KB |
Output is correct |
29 |
Correct |
7 ms |
50760 KB |
Output is correct |
30 |
Correct |
2 ms |
50760 KB |
Output is correct |
31 |
Correct |
3 ms |
50760 KB |
Output is correct |
32 |
Correct |
5 ms |
50928 KB |
Output is correct |
33 |
Correct |
7 ms |
50940 KB |
Output is correct |
34 |
Correct |
4 ms |
50940 KB |
Output is correct |
35 |
Correct |
11 ms |
51024 KB |
Output is correct |
36 |
Correct |
4 ms |
50760 KB |
Output is correct |
37 |
Correct |
6 ms |
51052 KB |
Output is correct |
38 |
Correct |
9 ms |
51176 KB |
Output is correct |
39 |
Correct |
7 ms |
51024 KB |
Output is correct |
40 |
Correct |
12 ms |
51180 KB |
Output is correct |
41 |
Correct |
4 ms |
51176 KB |
Output is correct |
42 |
Correct |
9 ms |
50924 KB |
Output is correct |
43 |
Correct |
8 ms |
50924 KB |
Output is correct |
44 |
Correct |
4 ms |
50920 KB |
Output is correct |
45 |
Correct |
13 ms |
51176 KB |
Output is correct |
46 |
Correct |
13 ms |
51176 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
50760 KB |
Output is correct |
2 |
Correct |
2 ms |
50760 KB |
Output is correct |
3 |
Correct |
1 ms |
50760 KB |
Output is correct |
4 |
Correct |
0 ms |
50760 KB |
Output is correct |
5 |
Correct |
1 ms |
50760 KB |
Output is correct |
6 |
Correct |
1 ms |
50760 KB |
Output is correct |
7 |
Correct |
1 ms |
50760 KB |
Output is correct |
8 |
Correct |
0 ms |
50760 KB |
Output is correct |
9 |
Correct |
1 ms |
50760 KB |
Output is correct |
10 |
Correct |
1 ms |
50760 KB |
Output is correct |
11 |
Correct |
0 ms |
50760 KB |
Output is correct |
12 |
Correct |
0 ms |
50760 KB |
Output is correct |
13 |
Correct |
1 ms |
50760 KB |
Output is correct |
14 |
Correct |
2 ms |
50760 KB |
Output is correct |
15 |
Correct |
0 ms |
50760 KB |
Output is correct |
16 |
Correct |
2 ms |
50760 KB |
Output is correct |
17 |
Correct |
1 ms |
50760 KB |
Output is correct |
18 |
Correct |
1 ms |
50760 KB |
Output is correct |
19 |
Correct |
4 ms |
50760 KB |
Output is correct |
20 |
Correct |
4 ms |
50760 KB |
Output is correct |
21 |
Correct |
0 ms |
50760 KB |
Output is correct |
22 |
Correct |
1 ms |
50760 KB |
Output is correct |
23 |
Correct |
2 ms |
50760 KB |
Output is correct |
24 |
Correct |
4 ms |
50760 KB |
Output is correct |
25 |
Correct |
3 ms |
50760 KB |
Output is correct |
26 |
Correct |
1 ms |
50760 KB |
Output is correct |
27 |
Correct |
0 ms |
50760 KB |
Output is correct |
28 |
Correct |
1 ms |
50760 KB |
Output is correct |
29 |
Correct |
3 ms |
50760 KB |
Output is correct |
30 |
Correct |
0 ms |
50760 KB |
Output is correct |
31 |
Correct |
3 ms |
50760 KB |
Output is correct |
32 |
Correct |
1 ms |
50928 KB |
Output is correct |
33 |
Correct |
11 ms |
50940 KB |
Output is correct |
34 |
Correct |
3 ms |
50940 KB |
Output is correct |
35 |
Correct |
10 ms |
51024 KB |
Output is correct |
36 |
Correct |
4 ms |
50760 KB |
Output is correct |
37 |
Correct |
6 ms |
51052 KB |
Output is correct |
38 |
Correct |
16 ms |
51176 KB |
Output is correct |
39 |
Correct |
10 ms |
51024 KB |
Output is correct |
40 |
Correct |
11 ms |
51180 KB |
Output is correct |
41 |
Correct |
4 ms |
51176 KB |
Output is correct |
42 |
Correct |
9 ms |
50924 KB |
Output is correct |
43 |
Correct |
6 ms |
50924 KB |
Output is correct |
44 |
Correct |
10 ms |
50920 KB |
Output is correct |
45 |
Correct |
22 ms |
51176 KB |
Output is correct |
46 |
Correct |
12 ms |
51176 KB |
Output is correct |
47 |
Correct |
20 ms |
51936 KB |
Output is correct |
48 |
Correct |
14 ms |
51156 KB |
Output is correct |
49 |
Correct |
19 ms |
51156 KB |
Output is correct |
50 |
Correct |
22 ms |
51024 KB |
Output is correct |
51 |
Correct |
22 ms |
52164 KB |
Output is correct |
52 |
Correct |
33 ms |
52168 KB |
Output is correct |
53 |
Correct |
16 ms |
51288 KB |
Output is correct |
54 |
Correct |
7 ms |
50760 KB |
Output is correct |
55 |
Correct |
14 ms |
50760 KB |
Output is correct |
56 |
Correct |
25 ms |
51684 KB |
Output is correct |
57 |
Correct |
7 ms |
50760 KB |
Output is correct |
58 |
Correct |
14 ms |
50920 KB |
Output is correct |
59 |
Correct |
29 ms |
50920 KB |
Output is correct |
60 |
Correct |
16 ms |
50928 KB |
Output is correct |
61 |
Correct |
29 ms |
50944 KB |
Output is correct |
62 |
Correct |
65 ms |
51684 KB |
Output is correct |
63 |
Correct |
52 ms |
54100 KB |
Output is correct |
64 |
Correct |
67 ms |
54076 KB |
Output is correct |
65 |
Correct |
273 ms |
54076 KB |
Output is correct |
66 |
Correct |
869 ms |
54096 KB |
Output is correct |
67 |
Correct |
340 ms |
54092 KB |
Output is correct |