#include <bits/stdc++.h>
#define fr first
#define sc second
#define mk make_pair
#define pb push_back
#define OK puts("OK");
#define sz(s) (int)s.size()
#define all(s) s.begin(), s.end()
using namespace std;
const int N = 2005;
int n, m, u[N][N], pos[30005], pw[30005], ans = 1e9 + 7;
vector <int> vec[N];
queue <pair <int, int> > q;
main()
{
cin >> n >> m;
for (int i = 0; i < m; i++)
{
scanf("%d%d", &pos[i], &pw[i]);
vec[pos[i]].pb(pw[i]);
}
for (auto to : vec[ pos[0] ])
{
q.push( mk(pos[0], to) );
u[ pos[0] ][to] = 1;
}
vec[ pos[0] ].clear();
while (!q.empty() )
{
int v = q.front().fr, cur_pow = q.front().sc;
q.pop();
for (auto it : vec[v])
{
if (!u[v][it])
{
u[v][it] = u[v][cur_pow];
q.push( mk(v, it) );
}
}
vec[v].clear();
if ( v - cur_pow >= 0 && !u[v - cur_pow][cur_pow] )
{
u[v - cur_pow][cur_pow] = u[v][cur_pow] + 1;
q.push( mk(v - cur_pow, cur_pow) );
}
if ( v + cur_pow < n && !u[v + cur_pow][cur_pow] )
{
u[v + cur_pow][cur_pow] = u[v][cur_pow] + 1;
q.push( mk(v + cur_pow, cur_pow) );
}
}
for (int i = 0; i < m; i++)
{
if (u[ pos[1] ][ pw[i] ])
ans = min(ans, u[ pos[1] ][ pw[i] ] - 1);
}
if (ans == 1e9 + 7) ans = -1;
cout << ans << endl;
}
/**
5 3
0 2
1 1
4 1
**/
Compilation message
skyscraper.cpp:21:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
main()
^
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:26:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &pos[i], &pw[i]);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
536 KB |
Output is correct |
4 |
Correct |
2 ms |
536 KB |
Output is correct |
5 |
Correct |
2 ms |
536 KB |
Output is correct |
6 |
Correct |
2 ms |
536 KB |
Output is correct |
7 |
Correct |
2 ms |
740 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
740 KB |
Output is correct |
2 |
Correct |
2 ms |
740 KB |
Output is correct |
3 |
Correct |
2 ms |
740 KB |
Output is correct |
4 |
Correct |
2 ms |
740 KB |
Output is correct |
5 |
Correct |
2 ms |
740 KB |
Output is correct |
6 |
Correct |
2 ms |
740 KB |
Output is correct |
7 |
Correct |
2 ms |
740 KB |
Output is correct |
8 |
Correct |
2 ms |
740 KB |
Output is correct |
9 |
Correct |
2 ms |
824 KB |
Output is correct |
10 |
Correct |
2 ms |
996 KB |
Output is correct |
11 |
Correct |
3 ms |
1124 KB |
Output is correct |
12 |
Correct |
3 ms |
1124 KB |
Output is correct |
13 |
Correct |
3 ms |
1124 KB |
Output is correct |
14 |
Correct |
4 ms |
1124 KB |
Output is correct |
15 |
Correct |
3 ms |
1124 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
1124 KB |
Output is correct |
2 |
Correct |
2 ms |
1124 KB |
Output is correct |
3 |
Correct |
2 ms |
1124 KB |
Output is correct |
4 |
Correct |
2 ms |
1124 KB |
Output is correct |
5 |
Correct |
2 ms |
1124 KB |
Output is correct |
6 |
Correct |
2 ms |
1124 KB |
Output is correct |
7 |
Correct |
2 ms |
1124 KB |
Output is correct |
8 |
Correct |
2 ms |
1124 KB |
Output is correct |
9 |
Correct |
2 ms |
1124 KB |
Output is correct |
10 |
Correct |
3 ms |
1124 KB |
Output is correct |
11 |
Correct |
3 ms |
1124 KB |
Output is correct |
12 |
Correct |
3 ms |
1124 KB |
Output is correct |
13 |
Correct |
3 ms |
1124 KB |
Output is correct |
14 |
Correct |
3 ms |
1124 KB |
Output is correct |
15 |
Correct |
4 ms |
1124 KB |
Output is correct |
16 |
Correct |
2 ms |
1124 KB |
Output is correct |
17 |
Correct |
7 ms |
4708 KB |
Output is correct |
18 |
Correct |
2 ms |
4708 KB |
Output is correct |
19 |
Correct |
2 ms |
4708 KB |
Output is correct |
20 |
Correct |
11 ms |
8676 KB |
Output is correct |
21 |
Correct |
3 ms |
8676 KB |
Output is correct |
22 |
Correct |
2 ms |
8676 KB |
Output is correct |
23 |
Correct |
12 ms |
9708 KB |
Output is correct |
24 |
Correct |
19 ms |
13436 KB |
Output is correct |
25 |
Correct |
16 ms |
14204 KB |
Output is correct |
26 |
Correct |
12 ms |
14204 KB |
Output is correct |
27 |
Correct |
12 ms |
14204 KB |
Output is correct |
28 |
Correct |
14 ms |
14204 KB |
Output is correct |
29 |
Correct |
12 ms |
14204 KB |
Output is correct |
30 |
Correct |
11 ms |
14204 KB |
Output is correct |
31 |
Correct |
13 ms |
14204 KB |
Output is correct |
32 |
Correct |
11 ms |
14204 KB |
Output is correct |
33 |
Correct |
14 ms |
14204 KB |
Output is correct |
34 |
Correct |
14 ms |
14204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
14204 KB |
Output is correct |
2 |
Correct |
2 ms |
14204 KB |
Output is correct |
3 |
Correct |
2 ms |
14204 KB |
Output is correct |
4 |
Correct |
2 ms |
14204 KB |
Output is correct |
5 |
Correct |
2 ms |
14204 KB |
Output is correct |
6 |
Correct |
2 ms |
14204 KB |
Output is correct |
7 |
Correct |
2 ms |
14204 KB |
Output is correct |
8 |
Correct |
2 ms |
14204 KB |
Output is correct |
9 |
Correct |
2 ms |
14204 KB |
Output is correct |
10 |
Correct |
3 ms |
14204 KB |
Output is correct |
11 |
Correct |
3 ms |
14204 KB |
Output is correct |
12 |
Correct |
3 ms |
14204 KB |
Output is correct |
13 |
Correct |
3 ms |
14204 KB |
Output is correct |
14 |
Correct |
3 ms |
14204 KB |
Output is correct |
15 |
Correct |
3 ms |
14204 KB |
Output is correct |
16 |
Correct |
2 ms |
14204 KB |
Output is correct |
17 |
Correct |
7 ms |
14204 KB |
Output is correct |
18 |
Correct |
2 ms |
14204 KB |
Output is correct |
19 |
Correct |
2 ms |
14204 KB |
Output is correct |
20 |
Correct |
11 ms |
14204 KB |
Output is correct |
21 |
Correct |
2 ms |
14204 KB |
Output is correct |
22 |
Correct |
2 ms |
14204 KB |
Output is correct |
23 |
Correct |
12 ms |
14204 KB |
Output is correct |
24 |
Correct |
16 ms |
14204 KB |
Output is correct |
25 |
Correct |
17 ms |
14204 KB |
Output is correct |
26 |
Correct |
11 ms |
14204 KB |
Output is correct |
27 |
Correct |
11 ms |
14204 KB |
Output is correct |
28 |
Correct |
14 ms |
14204 KB |
Output is correct |
29 |
Correct |
12 ms |
14204 KB |
Output is correct |
30 |
Correct |
11 ms |
14204 KB |
Output is correct |
31 |
Correct |
12 ms |
14204 KB |
Output is correct |
32 |
Correct |
11 ms |
14204 KB |
Output is correct |
33 |
Correct |
14 ms |
14204 KB |
Output is correct |
34 |
Correct |
14 ms |
14204 KB |
Output is correct |
35 |
Correct |
25 ms |
14204 KB |
Output is correct |
36 |
Correct |
12 ms |
14204 KB |
Output is correct |
37 |
Correct |
32 ms |
15612 KB |
Output is correct |
38 |
Correct |
35 ms |
17020 KB |
Output is correct |
39 |
Correct |
36 ms |
17020 KB |
Output is correct |
40 |
Correct |
35 ms |
17020 KB |
Output is correct |
41 |
Correct |
36 ms |
17024 KB |
Output is correct |
42 |
Correct |
16 ms |
17024 KB |
Output is correct |
43 |
Correct |
18 ms |
17024 KB |
Output is correct |
44 |
Correct |
16 ms |
17024 KB |
Output is correct |
45 |
Correct |
35 ms |
17024 KB |
Output is correct |
46 |
Correct |
33 ms |
17024 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
17024 KB |
Output is correct |
2 |
Correct |
2 ms |
17024 KB |
Output is correct |
3 |
Correct |
2 ms |
17024 KB |
Output is correct |
4 |
Correct |
2 ms |
17024 KB |
Output is correct |
5 |
Correct |
2 ms |
17024 KB |
Output is correct |
6 |
Correct |
2 ms |
17024 KB |
Output is correct |
7 |
Correct |
2 ms |
17024 KB |
Output is correct |
8 |
Correct |
2 ms |
17024 KB |
Output is correct |
9 |
Correct |
2 ms |
17024 KB |
Output is correct |
10 |
Correct |
2 ms |
17024 KB |
Output is correct |
11 |
Correct |
4 ms |
17024 KB |
Output is correct |
12 |
Correct |
2 ms |
17024 KB |
Output is correct |
13 |
Correct |
3 ms |
17024 KB |
Output is correct |
14 |
Correct |
3 ms |
17024 KB |
Output is correct |
15 |
Correct |
3 ms |
17024 KB |
Output is correct |
16 |
Correct |
3 ms |
17024 KB |
Output is correct |
17 |
Correct |
8 ms |
17024 KB |
Output is correct |
18 |
Correct |
2 ms |
17024 KB |
Output is correct |
19 |
Correct |
3 ms |
17024 KB |
Output is correct |
20 |
Correct |
11 ms |
17024 KB |
Output is correct |
21 |
Correct |
3 ms |
17024 KB |
Output is correct |
22 |
Correct |
2 ms |
17024 KB |
Output is correct |
23 |
Correct |
15 ms |
17024 KB |
Output is correct |
24 |
Correct |
17 ms |
17024 KB |
Output is correct |
25 |
Correct |
19 ms |
17024 KB |
Output is correct |
26 |
Correct |
13 ms |
17024 KB |
Output is correct |
27 |
Correct |
15 ms |
17024 KB |
Output is correct |
28 |
Correct |
14 ms |
17024 KB |
Output is correct |
29 |
Correct |
12 ms |
17024 KB |
Output is correct |
30 |
Correct |
11 ms |
17024 KB |
Output is correct |
31 |
Correct |
12 ms |
17024 KB |
Output is correct |
32 |
Correct |
11 ms |
17024 KB |
Output is correct |
33 |
Correct |
14 ms |
17024 KB |
Output is correct |
34 |
Correct |
19 ms |
17024 KB |
Output is correct |
35 |
Correct |
38 ms |
17024 KB |
Output is correct |
36 |
Correct |
14 ms |
17024 KB |
Output is correct |
37 |
Correct |
46 ms |
17024 KB |
Output is correct |
38 |
Correct |
37 ms |
17024 KB |
Output is correct |
39 |
Correct |
50 ms |
17276 KB |
Output is correct |
40 |
Correct |
38 ms |
17276 KB |
Output is correct |
41 |
Correct |
39 ms |
17276 KB |
Output is correct |
42 |
Correct |
18 ms |
17276 KB |
Output is correct |
43 |
Correct |
17 ms |
17276 KB |
Output is correct |
44 |
Correct |
15 ms |
17276 KB |
Output is correct |
45 |
Correct |
33 ms |
17276 KB |
Output is correct |
46 |
Correct |
47 ms |
17276 KB |
Output is correct |
47 |
Runtime error |
4 ms |
17276 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
48 |
Halted |
0 ms |
0 KB |
- |