#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
using namespace std;
using ll = long long;
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define MAX(a, b) (((a) < (b)) ? (b) : (a))
int const nmax = 30000;
int v[1 + nmax][2];
vector<int> g[1 + nmax];
map<pair<int,int>, int> dp;
int seen[1 + nmax];
queue<pair<int,int>> q;
void processlevel(int node, int cost){
if(seen[node] == 1)
return ;
seen[node] = 1;
for(int h = 0; h < g[node].size(); h++){
int jump2 = g[node][h];
if(dp.find({node, jump2}) == dp.end()){
dp[{node, jump2}] = cost;
q.push({node, jump2});
}
}
}
int main()
{
int n, m;
cin >> n >> m;
for(int i = 0;i < m; i++) {
cin >> v[i][0] >> v[i][1];
g[v[i][0]].push_back(v[i][1]);
}
processlevel(v[0][0], 0);
while(0 < q.size()){
int node = q.front().first;
int jump = q.front().second;
int cost = dp[{node, jump}];
q.pop();
if(0 <= node - jump)
if(dp.find({node - jump, jump}) == dp.end()) {
dp[{node - jump, jump}] = cost + 1;
processlevel(node - jump, cost + 1);
q.push({node - jump, jump});
}
if(node + jump < n)
if(dp.find({node + jump, jump}) == dp.end()) {
dp[{node + jump, jump}] = cost + 1;
processlevel(node + jump, cost + 1);
q.push({node + jump, jump});
}
}
if(dp.find({v[1][0], v[1][1]}) == dp.end())
cout << -1;
else
cout << dp[{v[1][0], v[1][1]}];
return 0;
}
Compilation message
skyscraper.cpp: In function 'void processlevel(int, int)':
skyscraper.cpp:25:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int h = 0; h < g[node].size(); h++){
~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
1024 KB |
Output is correct |
2 |
Correct |
5 ms |
1024 KB |
Output is correct |
3 |
Correct |
5 ms |
1024 KB |
Output is correct |
4 |
Correct |
5 ms |
1024 KB |
Output is correct |
5 |
Correct |
5 ms |
1024 KB |
Output is correct |
6 |
Correct |
5 ms |
1024 KB |
Output is correct |
7 |
Correct |
5 ms |
1024 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
1024 KB |
Output is correct |
2 |
Correct |
5 ms |
1024 KB |
Output is correct |
3 |
Correct |
5 ms |
1024 KB |
Output is correct |
4 |
Correct |
5 ms |
1024 KB |
Output is correct |
5 |
Correct |
5 ms |
1024 KB |
Output is correct |
6 |
Correct |
5 ms |
1024 KB |
Output is correct |
7 |
Correct |
5 ms |
1024 KB |
Output is correct |
8 |
Correct |
5 ms |
1024 KB |
Output is correct |
9 |
Correct |
5 ms |
1024 KB |
Output is correct |
10 |
Correct |
6 ms |
1152 KB |
Output is correct |
11 |
Correct |
8 ms |
1280 KB |
Output is correct |
12 |
Correct |
6 ms |
1152 KB |
Output is correct |
13 |
Correct |
6 ms |
1152 KB |
Output is correct |
14 |
Correct |
9 ms |
1408 KB |
Output is correct |
15 |
Correct |
9 ms |
1408 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
1024 KB |
Output is correct |
2 |
Correct |
5 ms |
1024 KB |
Output is correct |
3 |
Correct |
5 ms |
1024 KB |
Output is correct |
4 |
Correct |
5 ms |
1024 KB |
Output is correct |
5 |
Correct |
5 ms |
1024 KB |
Output is correct |
6 |
Correct |
5 ms |
1024 KB |
Output is correct |
7 |
Correct |
5 ms |
1024 KB |
Output is correct |
8 |
Correct |
5 ms |
1024 KB |
Output is correct |
9 |
Correct |
5 ms |
1024 KB |
Output is correct |
10 |
Correct |
6 ms |
1152 KB |
Output is correct |
11 |
Correct |
9 ms |
1280 KB |
Output is correct |
12 |
Correct |
6 ms |
1024 KB |
Output is correct |
13 |
Correct |
6 ms |
1024 KB |
Output is correct |
14 |
Correct |
9 ms |
1408 KB |
Output is correct |
15 |
Correct |
9 ms |
1408 KB |
Output is correct |
16 |
Correct |
6 ms |
1024 KB |
Output is correct |
17 |
Correct |
16 ms |
2048 KB |
Output is correct |
18 |
Correct |
6 ms |
1024 KB |
Output is correct |
19 |
Correct |
5 ms |
1024 KB |
Output is correct |
20 |
Correct |
6 ms |
1280 KB |
Output is correct |
21 |
Correct |
6 ms |
1024 KB |
Output is correct |
22 |
Correct |
5 ms |
1024 KB |
Output is correct |
23 |
Correct |
9 ms |
1408 KB |
Output is correct |
24 |
Correct |
15 ms |
1920 KB |
Output is correct |
25 |
Correct |
11 ms |
1536 KB |
Output is correct |
26 |
Correct |
10 ms |
1536 KB |
Output is correct |
27 |
Correct |
9 ms |
1536 KB |
Output is correct |
28 |
Correct |
20 ms |
2560 KB |
Output is correct |
29 |
Correct |
54 ms |
4856 KB |
Output is correct |
30 |
Correct |
15 ms |
2176 KB |
Output is correct |
31 |
Correct |
27 ms |
3192 KB |
Output is correct |
32 |
Correct |
20 ms |
2560 KB |
Output is correct |
33 |
Correct |
131 ms |
8544 KB |
Output is correct |
34 |
Correct |
121 ms |
8568 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
1024 KB |
Output is correct |
2 |
Correct |
5 ms |
1024 KB |
Output is correct |
3 |
Correct |
5 ms |
1024 KB |
Output is correct |
4 |
Correct |
5 ms |
1024 KB |
Output is correct |
5 |
Correct |
5 ms |
1024 KB |
Output is correct |
6 |
Correct |
5 ms |
1024 KB |
Output is correct |
7 |
Correct |
5 ms |
1024 KB |
Output is correct |
8 |
Correct |
5 ms |
1024 KB |
Output is correct |
9 |
Correct |
5 ms |
1024 KB |
Output is correct |
10 |
Correct |
6 ms |
1280 KB |
Output is correct |
11 |
Correct |
9 ms |
1280 KB |
Output is correct |
12 |
Correct |
6 ms |
1024 KB |
Output is correct |
13 |
Correct |
6 ms |
1024 KB |
Output is correct |
14 |
Correct |
9 ms |
1408 KB |
Output is correct |
15 |
Correct |
9 ms |
1408 KB |
Output is correct |
16 |
Correct |
6 ms |
1024 KB |
Output is correct |
17 |
Correct |
16 ms |
2048 KB |
Output is correct |
18 |
Correct |
6 ms |
1152 KB |
Output is correct |
19 |
Correct |
5 ms |
1024 KB |
Output is correct |
20 |
Correct |
7 ms |
1280 KB |
Output is correct |
21 |
Correct |
6 ms |
1024 KB |
Output is correct |
22 |
Correct |
5 ms |
1024 KB |
Output is correct |
23 |
Correct |
9 ms |
1408 KB |
Output is correct |
24 |
Correct |
14 ms |
2048 KB |
Output is correct |
25 |
Correct |
10 ms |
1664 KB |
Output is correct |
26 |
Correct |
10 ms |
1536 KB |
Output is correct |
27 |
Correct |
9 ms |
1536 KB |
Output is correct |
28 |
Correct |
19 ms |
2560 KB |
Output is correct |
29 |
Correct |
51 ms |
4728 KB |
Output is correct |
30 |
Correct |
15 ms |
2176 KB |
Output is correct |
31 |
Correct |
28 ms |
3072 KB |
Output is correct |
32 |
Correct |
19 ms |
2560 KB |
Output is correct |
33 |
Correct |
124 ms |
8568 KB |
Output is correct |
34 |
Correct |
133 ms |
8568 KB |
Output is correct |
35 |
Correct |
119 ms |
8184 KB |
Output is correct |
36 |
Correct |
20 ms |
2304 KB |
Output is correct |
37 |
Correct |
237 ms |
12412 KB |
Output is correct |
38 |
Correct |
240 ms |
11896 KB |
Output is correct |
39 |
Correct |
218 ms |
11896 KB |
Output is correct |
40 |
Correct |
215 ms |
11812 KB |
Output is correct |
41 |
Correct |
215 ms |
11768 KB |
Output is correct |
42 |
Correct |
28 ms |
2056 KB |
Output is correct |
43 |
Correct |
25 ms |
2048 KB |
Output is correct |
44 |
Correct |
25 ms |
1792 KB |
Output is correct |
45 |
Correct |
827 ms |
32024 KB |
Output is correct |
46 |
Correct |
800 ms |
31864 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
1024 KB |
Output is correct |
2 |
Correct |
5 ms |
1024 KB |
Output is correct |
3 |
Correct |
5 ms |
1024 KB |
Output is correct |
4 |
Correct |
5 ms |
1024 KB |
Output is correct |
5 |
Correct |
5 ms |
1024 KB |
Output is correct |
6 |
Correct |
5 ms |
1024 KB |
Output is correct |
7 |
Correct |
5 ms |
1152 KB |
Output is correct |
8 |
Correct |
5 ms |
1024 KB |
Output is correct |
9 |
Correct |
5 ms |
1024 KB |
Output is correct |
10 |
Correct |
6 ms |
1152 KB |
Output is correct |
11 |
Correct |
9 ms |
1280 KB |
Output is correct |
12 |
Correct |
6 ms |
1024 KB |
Output is correct |
13 |
Correct |
6 ms |
1024 KB |
Output is correct |
14 |
Correct |
9 ms |
1408 KB |
Output is correct |
15 |
Correct |
9 ms |
1408 KB |
Output is correct |
16 |
Correct |
5 ms |
1024 KB |
Output is correct |
17 |
Correct |
16 ms |
2048 KB |
Output is correct |
18 |
Correct |
6 ms |
1152 KB |
Output is correct |
19 |
Correct |
5 ms |
1024 KB |
Output is correct |
20 |
Correct |
7 ms |
1280 KB |
Output is correct |
21 |
Correct |
6 ms |
1024 KB |
Output is correct |
22 |
Correct |
5 ms |
1024 KB |
Output is correct |
23 |
Correct |
9 ms |
1408 KB |
Output is correct |
24 |
Correct |
14 ms |
1920 KB |
Output is correct |
25 |
Correct |
10 ms |
1536 KB |
Output is correct |
26 |
Correct |
10 ms |
1536 KB |
Output is correct |
27 |
Correct |
9 ms |
1536 KB |
Output is correct |
28 |
Correct |
19 ms |
2560 KB |
Output is correct |
29 |
Correct |
53 ms |
4856 KB |
Output is correct |
30 |
Correct |
16 ms |
2176 KB |
Output is correct |
31 |
Correct |
28 ms |
3200 KB |
Output is correct |
32 |
Correct |
19 ms |
2560 KB |
Output is correct |
33 |
Correct |
120 ms |
8572 KB |
Output is correct |
34 |
Correct |
126 ms |
8568 KB |
Output is correct |
35 |
Correct |
119 ms |
8056 KB |
Output is correct |
36 |
Correct |
20 ms |
2304 KB |
Output is correct |
37 |
Correct |
225 ms |
12280 KB |
Output is correct |
38 |
Correct |
220 ms |
11864 KB |
Output is correct |
39 |
Correct |
238 ms |
11896 KB |
Output is correct |
40 |
Correct |
243 ms |
11896 KB |
Output is correct |
41 |
Correct |
225 ms |
11840 KB |
Output is correct |
42 |
Correct |
27 ms |
2048 KB |
Output is correct |
43 |
Correct |
25 ms |
2048 KB |
Output is correct |
44 |
Correct |
24 ms |
1784 KB |
Output is correct |
45 |
Correct |
782 ms |
31992 KB |
Output is correct |
46 |
Correct |
839 ms |
32248 KB |
Output is correct |
47 |
Execution timed out |
1091 ms |
34688 KB |
Time limit exceeded |
48 |
Halted |
0 ms |
0 KB |
- |