#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[N], pw[N], 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]);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
484 KB |
Output is correct |
3 |
Correct |
2 ms |
484 KB |
Output is correct |
4 |
Correct |
2 ms |
496 KB |
Output is correct |
5 |
Correct |
2 ms |
608 KB |
Output is correct |
6 |
Correct |
3 ms |
608 KB |
Output is correct |
7 |
Correct |
2 ms |
608 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
608 KB |
Output is correct |
2 |
Correct |
2 ms |
700 KB |
Output is correct |
3 |
Correct |
2 ms |
700 KB |
Output is correct |
4 |
Correct |
2 ms |
700 KB |
Output is correct |
5 |
Correct |
3 ms |
724 KB |
Output is correct |
6 |
Correct |
2 ms |
724 KB |
Output is correct |
7 |
Correct |
2 ms |
724 KB |
Output is correct |
8 |
Correct |
2 ms |
740 KB |
Output is correct |
9 |
Correct |
2 ms |
868 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 |
3 ms |
1124 KB |
Output is correct |
15 |
Correct |
4 ms |
1124 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
3 ms |
1124 KB |
Output is correct |
10 |
Correct |
3 ms |
1124 KB |
Output is correct |
11 |
Correct |
4 ms |
1132 KB |
Output is correct |
12 |
Correct |
3 ms |
1132 KB |
Output is correct |
13 |
Correct |
3 ms |
1132 KB |
Output is correct |
14 |
Correct |
3 ms |
1132 KB |
Output is correct |
15 |
Correct |
3 ms |
1132 KB |
Output is correct |
16 |
Correct |
2 ms |
1132 KB |
Output is correct |
17 |
Correct |
9 ms |
4604 KB |
Output is correct |
18 |
Correct |
2 ms |
4604 KB |
Output is correct |
19 |
Correct |
3 ms |
4604 KB |
Output is correct |
20 |
Correct |
11 ms |
8700 KB |
Output is correct |
21 |
Correct |
2 ms |
8700 KB |
Output is correct |
22 |
Correct |
2 ms |
8700 KB |
Output is correct |
23 |
Correct |
13 ms |
9724 KB |
Output is correct |
24 |
Correct |
17 ms |
13308 KB |
Output is correct |
25 |
Correct |
17 ms |
14148 KB |
Output is correct |
26 |
Correct |
11 ms |
14148 KB |
Output is correct |
27 |
Correct |
12 ms |
14148 KB |
Output is correct |
28 |
Correct |
15 ms |
14148 KB |
Output is correct |
29 |
Correct |
12 ms |
14148 KB |
Output is correct |
30 |
Correct |
12 ms |
14148 KB |
Output is correct |
31 |
Correct |
11 ms |
14148 KB |
Output is correct |
32 |
Correct |
12 ms |
14148 KB |
Output is correct |
33 |
Correct |
19 ms |
14148 KB |
Output is correct |
34 |
Correct |
14 ms |
14148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
14148 KB |
Output is correct |
2 |
Correct |
2 ms |
14148 KB |
Output is correct |
3 |
Correct |
2 ms |
14148 KB |
Output is correct |
4 |
Correct |
2 ms |
14148 KB |
Output is correct |
5 |
Correct |
2 ms |
14148 KB |
Output is correct |
6 |
Correct |
2 ms |
14148 KB |
Output is correct |
7 |
Correct |
2 ms |
14148 KB |
Output is correct |
8 |
Correct |
2 ms |
14148 KB |
Output is correct |
9 |
Correct |
2 ms |
14148 KB |
Output is correct |
10 |
Correct |
3 ms |
14148 KB |
Output is correct |
11 |
Correct |
3 ms |
14148 KB |
Output is correct |
12 |
Correct |
3 ms |
14148 KB |
Output is correct |
13 |
Correct |
3 ms |
14148 KB |
Output is correct |
14 |
Correct |
3 ms |
14148 KB |
Output is correct |
15 |
Correct |
4 ms |
14148 KB |
Output is correct |
16 |
Correct |
2 ms |
14148 KB |
Output is correct |
17 |
Correct |
7 ms |
14148 KB |
Output is correct |
18 |
Correct |
3 ms |
14148 KB |
Output is correct |
19 |
Correct |
2 ms |
14148 KB |
Output is correct |
20 |
Correct |
12 ms |
14148 KB |
Output is correct |
21 |
Correct |
2 ms |
14148 KB |
Output is correct |
22 |
Correct |
3 ms |
14148 KB |
Output is correct |
23 |
Correct |
12 ms |
14148 KB |
Output is correct |
24 |
Correct |
18 ms |
14148 KB |
Output is correct |
25 |
Correct |
18 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 |
15 ms |
14204 KB |
Output is correct |
29 |
Correct |
15 ms |
14204 KB |
Output is correct |
30 |
Correct |
11 ms |
14204 KB |
Output is correct |
31 |
Correct |
14 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 |
Incorrect |
26 ms |
14204 KB |
Output isn't correct |
36 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
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 |
4 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 |
4 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 |
12 ms |
14204 KB |
Output is correct |
27 |
Correct |
12 ms |
14204 KB |
Output is correct |
28 |
Correct |
15 ms |
14204 KB |
Output is correct |
29 |
Correct |
16 ms |
14204 KB |
Output is correct |
30 |
Correct |
12 ms |
14204 KB |
Output is correct |
31 |
Correct |
14 ms |
14204 KB |
Output is correct |
32 |
Correct |
13 ms |
14204 KB |
Output is correct |
33 |
Correct |
15 ms |
14204 KB |
Output is correct |
34 |
Correct |
20 ms |
14204 KB |
Output is correct |
35 |
Incorrect |
26 ms |
14204 KB |
Output isn't correct |
36 |
Halted |
0 ms |
0 KB |
- |