#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 3e4 + 5;
const int sqr = 105;
int n, m, s, t, dp[N];
bool vs[sqr][N];
vector<int> g[N];
struct node {
int vt, step, cost;
node(int vt, int step, int cost) : vt(vt), step(step), cost(cost) {}
};
void bfs(int u) {
memset(dp, -1, sizeof(dp));
queue<node> q; q.emplace(u, 0, 0);
while(q.size()) {
auto [vt, step, cost] = q.front(); q.pop();
if(vt < 0 || vt >= n) continue;
int p = abs(step);
if(p < sqr && vs[p][vt]) continue;
if(p < sqr) vs[p][vt] = 1;
q.emplace(vt + step, step, cost + 1);
if(dp[vt] == -1) {
dp[vt] = cost;
for(int power : g[vt]) {
q.emplace(vt + power, power, cost + 1);
q.emplace(vt - power, -power, cost + 1);
}
}
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n >> m;
for(int i = 0; i < m; ++i) {
int vt, step; cin >> vt >> step;
g[vt].push_back(step);
if(i == 0) s = vt;
if(i == 1) t = vt;
}
bfs(s);
cout << dp[t] << '\n';
return 0;
}
Compilation message
skyscraper.cpp: In function 'void bfs(int)':
skyscraper.cpp:20:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
20 | auto [vt, step, cost] = q.front(); q.pop();
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2656 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
2692 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2692 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
1 ms |
2692 KB |
Output is correct |
7 |
Correct |
1 ms |
2652 KB |
Output is correct |
8 |
Correct |
1 ms |
2692 KB |
Output is correct |
9 |
Correct |
1 ms |
2692 KB |
Output is correct |
10 |
Correct |
1 ms |
2908 KB |
Output is correct |
11 |
Correct |
2 ms |
2908 KB |
Output is correct |
12 |
Correct |
1 ms |
2752 KB |
Output is correct |
13 |
Correct |
1 ms |
2652 KB |
Output is correct |
14 |
Correct |
1 ms |
2700 KB |
Output is correct |
15 |
Correct |
1 ms |
2908 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2692 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
1 ms |
2696 KB |
Output is correct |
7 |
Correct |
1 ms |
2652 KB |
Output is correct |
8 |
Correct |
1 ms |
2652 KB |
Output is correct |
9 |
Correct |
1 ms |
2692 KB |
Output is correct |
10 |
Correct |
1 ms |
2908 KB |
Output is correct |
11 |
Correct |
2 ms |
2908 KB |
Output is correct |
12 |
Correct |
1 ms |
2908 KB |
Output is correct |
13 |
Correct |
1 ms |
2648 KB |
Output is correct |
14 |
Correct |
1 ms |
2908 KB |
Output is correct |
15 |
Correct |
1 ms |
2908 KB |
Output is correct |
16 |
Correct |
1 ms |
2652 KB |
Output is correct |
17 |
Correct |
2 ms |
2908 KB |
Output is correct |
18 |
Correct |
1 ms |
2652 KB |
Output is correct |
19 |
Correct |
1 ms |
2652 KB |
Output is correct |
20 |
Correct |
1 ms |
2648 KB |
Output is correct |
21 |
Correct |
1 ms |
2696 KB |
Output is correct |
22 |
Correct |
1 ms |
2652 KB |
Output is correct |
23 |
Correct |
1 ms |
2956 KB |
Output is correct |
24 |
Correct |
2 ms |
2908 KB |
Output is correct |
25 |
Correct |
2 ms |
2652 KB |
Output is correct |
26 |
Correct |
2 ms |
2904 KB |
Output is correct |
27 |
Correct |
1 ms |
2908 KB |
Output is correct |
28 |
Correct |
2 ms |
2960 KB |
Output is correct |
29 |
Correct |
2 ms |
2652 KB |
Output is correct |
30 |
Correct |
1 ms |
2652 KB |
Output is correct |
31 |
Correct |
1 ms |
2652 KB |
Output is correct |
32 |
Correct |
1 ms |
2652 KB |
Output is correct |
33 |
Correct |
2 ms |
2908 KB |
Output is correct |
34 |
Correct |
2 ms |
2908 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2688 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
2652 KB |
Output is correct |
8 |
Correct |
1 ms |
2652 KB |
Output is correct |
9 |
Correct |
1 ms |
2648 KB |
Output is correct |
10 |
Correct |
1 ms |
2904 KB |
Output is correct |
11 |
Correct |
1 ms |
2908 KB |
Output is correct |
12 |
Correct |
1 ms |
2908 KB |
Output is correct |
13 |
Correct |
1 ms |
2652 KB |
Output is correct |
14 |
Correct |
1 ms |
2908 KB |
Output is correct |
15 |
Correct |
1 ms |
2908 KB |
Output is correct |
16 |
Correct |
1 ms |
2652 KB |
Output is correct |
17 |
Correct |
1 ms |
2960 KB |
Output is correct |
18 |
Correct |
1 ms |
2652 KB |
Output is correct |
19 |
Correct |
1 ms |
2652 KB |
Output is correct |
20 |
Correct |
1 ms |
2648 KB |
Output is correct |
21 |
Correct |
1 ms |
2908 KB |
Output is correct |
22 |
Correct |
1 ms |
2652 KB |
Output is correct |
23 |
Correct |
1 ms |
2956 KB |
Output is correct |
24 |
Correct |
1 ms |
2908 KB |
Output is correct |
25 |
Correct |
1 ms |
2652 KB |
Output is correct |
26 |
Correct |
1 ms |
2908 KB |
Output is correct |
27 |
Correct |
1 ms |
2908 KB |
Output is correct |
28 |
Correct |
1 ms |
2908 KB |
Output is correct |
29 |
Correct |
1 ms |
2652 KB |
Output is correct |
30 |
Correct |
1 ms |
2652 KB |
Output is correct |
31 |
Correct |
2 ms |
2652 KB |
Output is correct |
32 |
Correct |
1 ms |
2652 KB |
Output is correct |
33 |
Correct |
2 ms |
2908 KB |
Output is correct |
34 |
Correct |
2 ms |
2952 KB |
Output is correct |
35 |
Correct |
6 ms |
3932 KB |
Output is correct |
36 |
Correct |
2 ms |
3160 KB |
Output is correct |
37 |
Correct |
6 ms |
3672 KB |
Output is correct |
38 |
Correct |
11 ms |
4184 KB |
Output is correct |
39 |
Correct |
8 ms |
4188 KB |
Output is correct |
40 |
Correct |
8 ms |
4104 KB |
Output is correct |
41 |
Correct |
8 ms |
4188 KB |
Output is correct |
42 |
Correct |
6 ms |
3932 KB |
Output is correct |
43 |
Correct |
5 ms |
3932 KB |
Output is correct |
44 |
Correct |
5 ms |
3932 KB |
Output is correct |
45 |
Correct |
10 ms |
4188 KB |
Output is correct |
46 |
Correct |
9 ms |
4184 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
2652 KB |
Output is correct |
8 |
Correct |
1 ms |
2660 KB |
Output is correct |
9 |
Correct |
1 ms |
2664 KB |
Output is correct |
10 |
Correct |
1 ms |
2920 KB |
Output is correct |
11 |
Correct |
1 ms |
2916 KB |
Output is correct |
12 |
Correct |
1 ms |
2920 KB |
Output is correct |
13 |
Correct |
1 ms |
2664 KB |
Output is correct |
14 |
Correct |
1 ms |
2920 KB |
Output is correct |
15 |
Correct |
1 ms |
2908 KB |
Output is correct |
16 |
Correct |
1 ms |
2648 KB |
Output is correct |
17 |
Correct |
1 ms |
2904 KB |
Output is correct |
18 |
Correct |
1 ms |
2652 KB |
Output is correct |
19 |
Correct |
1 ms |
2652 KB |
Output is correct |
20 |
Correct |
3 ms |
2652 KB |
Output is correct |
21 |
Correct |
1 ms |
2648 KB |
Output is correct |
22 |
Correct |
1 ms |
2652 KB |
Output is correct |
23 |
Correct |
1 ms |
2908 KB |
Output is correct |
24 |
Correct |
2 ms |
2908 KB |
Output is correct |
25 |
Correct |
1 ms |
2652 KB |
Output is correct |
26 |
Correct |
1 ms |
2904 KB |
Output is correct |
27 |
Correct |
1 ms |
2908 KB |
Output is correct |
28 |
Correct |
2 ms |
2908 KB |
Output is correct |
29 |
Correct |
1 ms |
2652 KB |
Output is correct |
30 |
Correct |
1 ms |
2652 KB |
Output is correct |
31 |
Correct |
1 ms |
2652 KB |
Output is correct |
32 |
Correct |
1 ms |
2652 KB |
Output is correct |
33 |
Correct |
2 ms |
2904 KB |
Output is correct |
34 |
Correct |
2 ms |
2904 KB |
Output is correct |
35 |
Correct |
6 ms |
3864 KB |
Output is correct |
36 |
Correct |
3 ms |
3164 KB |
Output is correct |
37 |
Correct |
7 ms |
3904 KB |
Output is correct |
38 |
Correct |
8 ms |
4188 KB |
Output is correct |
39 |
Correct |
11 ms |
4188 KB |
Output is correct |
40 |
Correct |
8 ms |
4188 KB |
Output is correct |
41 |
Correct |
8 ms |
4188 KB |
Output is correct |
42 |
Correct |
5 ms |
3796 KB |
Output is correct |
43 |
Correct |
5 ms |
3928 KB |
Output is correct |
44 |
Correct |
6 ms |
3732 KB |
Output is correct |
45 |
Correct |
9 ms |
4188 KB |
Output is correct |
46 |
Correct |
13 ms |
4188 KB |
Output is correct |
47 |
Correct |
13 ms |
4700 KB |
Output is correct |
48 |
Correct |
6 ms |
3360 KB |
Output is correct |
49 |
Correct |
5 ms |
3416 KB |
Output is correct |
50 |
Correct |
3 ms |
3184 KB |
Output is correct |
51 |
Correct |
12 ms |
4716 KB |
Output is correct |
52 |
Correct |
11 ms |
4956 KB |
Output is correct |
53 |
Correct |
8 ms |
3676 KB |
Output is correct |
54 |
Correct |
1 ms |
2652 KB |
Output is correct |
55 |
Correct |
1 ms |
2652 KB |
Output is correct |
56 |
Correct |
6 ms |
3928 KB |
Output is correct |
57 |
Correct |
5 ms |
2908 KB |
Output is correct |
58 |
Correct |
5 ms |
3600 KB |
Output is correct |
59 |
Correct |
6 ms |
3932 KB |
Output is correct |
60 |
Correct |
7 ms |
4700 KB |
Output is correct |
61 |
Correct |
6 ms |
4188 KB |
Output is correct |
62 |
Correct |
13 ms |
4700 KB |
Output is correct |
63 |
Correct |
27 ms |
3580 KB |
Output is correct |
64 |
Correct |
26 ms |
3416 KB |
Output is correct |
65 |
Correct |
39 ms |
3668 KB |
Output is correct |
66 |
Correct |
77 ms |
5456 KB |
Output is correct |
67 |
Correct |
106 ms |
5272 KB |
Output is correct |