#include <bits/stdc++.h>
#define ll long long
#define mp make_pair
using namespace std;
const int maxn = 2100;
int dist[maxn][maxn];
bool visited[maxn][maxn];
int st[30100], p[30100], n, m;
set<int>ps[maxn];
int main() {
cin>>n>>m;
for(int i=0;i<m;i++) {
cin>>st[i]>>p[i];
ps[st[i]].insert(p[i]);
}
queue<pair<int,int> > q;
q.push(mp(st[0], p[0]));
visited[st[0]][0] = true;
visited[st[0]][p[0]]=true;
while(!q.empty()) {
int currx = q.front().first;
int currc = q.front().second;
q.pop();
if(currx - currc >= 0 && !visited[currx-currc][currc]) {
visited[currx-currc][currc] = true;
dist[currx-currc][currc] = dist[currx][currc] + 1;
q.push(mp(currx-currc, currc));
}
if(currx + currc < n && !visited[currx+currc][currc]) {
visited[currx+currc][currc] = true;
dist[currx+currc][currc] = dist[currx][currc] + 1;
q.push(mp(currx+currc, currc));
}
for(int i: ps[currx]) {
if(currx - i >= 0 && !visited[currx-i][i]) {
visited[currx-i][i] = true;
dist[currx-i][i] = dist[currx][currc] + 1;
q.push(mp(currx-i, i));
}
if(currx + i < n && !visited[currx+i][i]) {
visited[currx+i][i] = true;
dist[currx+i][i] = dist[currx][currc] + 1;
q.push(mp(currx+i, i));
}
}
}
int result = INT_MAX;
for(int i=0;i<maxn;i++) {
if(visited[st[1]][i]) result = min(result, dist[st[1]][i]);
}
if(result == INT_MAX) cout<<"-1\n";
else cout<<result<<"\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
484 KB |
Output is correct |
3 |
Correct |
2 ms |
520 KB |
Output is correct |
4 |
Correct |
2 ms |
520 KB |
Output is correct |
5 |
Correct |
2 ms |
552 KB |
Output is correct |
6 |
Correct |
2 ms |
600 KB |
Output is correct |
7 |
Correct |
3 ms |
752 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
752 KB |
Output is correct |
2 |
Correct |
2 ms |
752 KB |
Output is correct |
3 |
Correct |
2 ms |
752 KB |
Output is correct |
4 |
Correct |
2 ms |
752 KB |
Output is correct |
5 |
Correct |
2 ms |
752 KB |
Output is correct |
6 |
Correct |
2 ms |
752 KB |
Output is correct |
7 |
Correct |
2 ms |
752 KB |
Output is correct |
8 |
Correct |
3 ms |
1004 KB |
Output is correct |
9 |
Correct |
3 ms |
1020 KB |
Output is correct |
10 |
Correct |
3 ms |
1316 KB |
Output is correct |
11 |
Correct |
5 ms |
1404 KB |
Output is correct |
12 |
Correct |
4 ms |
1404 KB |
Output is correct |
13 |
Correct |
4 ms |
1404 KB |
Output is correct |
14 |
Correct |
5 ms |
1404 KB |
Output is correct |
15 |
Correct |
6 ms |
1404 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
1404 KB |
Output is correct |
2 |
Correct |
2 ms |
1404 KB |
Output is correct |
3 |
Correct |
2 ms |
1404 KB |
Output is correct |
4 |
Correct |
2 ms |
1404 KB |
Output is correct |
5 |
Correct |
2 ms |
1404 KB |
Output is correct |
6 |
Correct |
3 ms |
1404 KB |
Output is correct |
7 |
Correct |
2 ms |
1404 KB |
Output is correct |
8 |
Correct |
3 ms |
1404 KB |
Output is correct |
9 |
Correct |
3 ms |
1404 KB |
Output is correct |
10 |
Correct |
3 ms |
1404 KB |
Output is correct |
11 |
Correct |
5 ms |
1408 KB |
Output is correct |
12 |
Correct |
4 ms |
1408 KB |
Output is correct |
13 |
Correct |
4 ms |
1408 KB |
Output is correct |
14 |
Correct |
5 ms |
1408 KB |
Output is correct |
15 |
Correct |
6 ms |
1408 KB |
Output is correct |
16 |
Correct |
4 ms |
1408 KB |
Output is correct |
17 |
Correct |
10 ms |
6264 KB |
Output is correct |
18 |
Correct |
4 ms |
6264 KB |
Output is correct |
19 |
Correct |
3 ms |
6264 KB |
Output is correct |
20 |
Correct |
16 ms |
12924 KB |
Output is correct |
21 |
Correct |
3 ms |
12924 KB |
Output is correct |
22 |
Correct |
3 ms |
12924 KB |
Output is correct |
23 |
Correct |
15 ms |
12924 KB |
Output is correct |
24 |
Correct |
16 ms |
17148 KB |
Output is correct |
25 |
Correct |
17 ms |
18432 KB |
Output is correct |
26 |
Correct |
13 ms |
18432 KB |
Output is correct |
27 |
Correct |
14 ms |
18432 KB |
Output is correct |
28 |
Correct |
16 ms |
18432 KB |
Output is correct |
29 |
Correct |
13 ms |
18432 KB |
Output is correct |
30 |
Correct |
12 ms |
18432 KB |
Output is correct |
31 |
Correct |
14 ms |
18432 KB |
Output is correct |
32 |
Correct |
13 ms |
18432 KB |
Output is correct |
33 |
Correct |
21 ms |
18432 KB |
Output is correct |
34 |
Correct |
17 ms |
18432 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
18432 KB |
Output is correct |
2 |
Correct |
2 ms |
18432 KB |
Output is correct |
3 |
Correct |
3 ms |
18432 KB |
Output is correct |
4 |
Correct |
2 ms |
18432 KB |
Output is correct |
5 |
Correct |
2 ms |
18432 KB |
Output is correct |
6 |
Correct |
2 ms |
18432 KB |
Output is correct |
7 |
Correct |
3 ms |
18432 KB |
Output is correct |
8 |
Correct |
2 ms |
18432 KB |
Output is correct |
9 |
Correct |
2 ms |
18432 KB |
Output is correct |
10 |
Correct |
4 ms |
18432 KB |
Output is correct |
11 |
Correct |
12 ms |
18432 KB |
Output is correct |
12 |
Correct |
4 ms |
18432 KB |
Output is correct |
13 |
Correct |
3 ms |
18432 KB |
Output is correct |
14 |
Correct |
5 ms |
18432 KB |
Output is correct |
15 |
Correct |
7 ms |
18432 KB |
Output is correct |
16 |
Correct |
3 ms |
18432 KB |
Output is correct |
17 |
Correct |
10 ms |
18432 KB |
Output is correct |
18 |
Correct |
3 ms |
18432 KB |
Output is correct |
19 |
Correct |
3 ms |
18432 KB |
Output is correct |
20 |
Correct |
14 ms |
18432 KB |
Output is correct |
21 |
Correct |
3 ms |
18432 KB |
Output is correct |
22 |
Correct |
4 ms |
18432 KB |
Output is correct |
23 |
Correct |
16 ms |
18432 KB |
Output is correct |
24 |
Correct |
19 ms |
18432 KB |
Output is correct |
25 |
Correct |
20 ms |
18432 KB |
Output is correct |
26 |
Correct |
15 ms |
18432 KB |
Output is correct |
27 |
Correct |
18 ms |
18432 KB |
Output is correct |
28 |
Correct |
19 ms |
18432 KB |
Output is correct |
29 |
Correct |
15 ms |
18432 KB |
Output is correct |
30 |
Correct |
14 ms |
18432 KB |
Output is correct |
31 |
Correct |
17 ms |
18432 KB |
Output is correct |
32 |
Correct |
17 ms |
18432 KB |
Output is correct |
33 |
Correct |
17 ms |
18432 KB |
Output is correct |
34 |
Correct |
18 ms |
18432 KB |
Output is correct |
35 |
Correct |
87 ms |
18432 KB |
Output is correct |
36 |
Correct |
14 ms |
18432 KB |
Output is correct |
37 |
Correct |
117 ms |
20804 KB |
Output is correct |
38 |
Correct |
179 ms |
23612 KB |
Output is correct |
39 |
Correct |
135 ms |
23948 KB |
Output is correct |
40 |
Correct |
161 ms |
24116 KB |
Output is correct |
41 |
Correct |
167 ms |
24484 KB |
Output is correct |
42 |
Correct |
46 ms |
24484 KB |
Output is correct |
43 |
Correct |
31 ms |
24484 KB |
Output is correct |
44 |
Correct |
31 ms |
24484 KB |
Output is correct |
45 |
Correct |
226 ms |
24484 KB |
Output is correct |
46 |
Correct |
228 ms |
24484 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
24484 KB |
Output is correct |
2 |
Correct |
3 ms |
24484 KB |
Output is correct |
3 |
Correct |
3 ms |
24484 KB |
Output is correct |
4 |
Correct |
3 ms |
24484 KB |
Output is correct |
5 |
Correct |
3 ms |
24484 KB |
Output is correct |
6 |
Correct |
2 ms |
24484 KB |
Output is correct |
7 |
Correct |
2 ms |
24484 KB |
Output is correct |
8 |
Correct |
2 ms |
24484 KB |
Output is correct |
9 |
Correct |
3 ms |
24484 KB |
Output is correct |
10 |
Correct |
4 ms |
24484 KB |
Output is correct |
11 |
Correct |
5 ms |
24484 KB |
Output is correct |
12 |
Correct |
5 ms |
24484 KB |
Output is correct |
13 |
Correct |
5 ms |
24484 KB |
Output is correct |
14 |
Correct |
6 ms |
24484 KB |
Output is correct |
15 |
Correct |
5 ms |
24484 KB |
Output is correct |
16 |
Correct |
3 ms |
24484 KB |
Output is correct |
17 |
Correct |
11 ms |
24484 KB |
Output is correct |
18 |
Correct |
3 ms |
24484 KB |
Output is correct |
19 |
Correct |
3 ms |
24484 KB |
Output is correct |
20 |
Correct |
14 ms |
24484 KB |
Output is correct |
21 |
Correct |
3 ms |
24484 KB |
Output is correct |
22 |
Correct |
4 ms |
24484 KB |
Output is correct |
23 |
Correct |
14 ms |
24484 KB |
Output is correct |
24 |
Correct |
18 ms |
24484 KB |
Output is correct |
25 |
Correct |
20 ms |
24484 KB |
Output is correct |
26 |
Correct |
16 ms |
24484 KB |
Output is correct |
27 |
Correct |
16 ms |
24484 KB |
Output is correct |
28 |
Correct |
19 ms |
24484 KB |
Output is correct |
29 |
Correct |
16 ms |
24484 KB |
Output is correct |
30 |
Correct |
13 ms |
24484 KB |
Output is correct |
31 |
Correct |
16 ms |
24484 KB |
Output is correct |
32 |
Correct |
14 ms |
24484 KB |
Output is correct |
33 |
Correct |
20 ms |
24484 KB |
Output is correct |
34 |
Correct |
21 ms |
24484 KB |
Output is correct |
35 |
Correct |
92 ms |
24484 KB |
Output is correct |
36 |
Correct |
20 ms |
24484 KB |
Output is correct |
37 |
Correct |
98 ms |
24484 KB |
Output is correct |
38 |
Correct |
133 ms |
25940 KB |
Output is correct |
39 |
Correct |
164 ms |
26276 KB |
Output is correct |
40 |
Correct |
130 ms |
26468 KB |
Output is correct |
41 |
Correct |
134 ms |
26748 KB |
Output is correct |
42 |
Correct |
33 ms |
26748 KB |
Output is correct |
43 |
Correct |
34 ms |
26748 KB |
Output is correct |
44 |
Correct |
31 ms |
26748 KB |
Output is correct |
45 |
Correct |
204 ms |
26748 KB |
Output is correct |
46 |
Correct |
219 ms |
26748 KB |
Output is correct |
47 |
Runtime error |
4 ms |
26748 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
48 |
Halted |
0 ms |
0 KB |
- |