#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[maxn], p[maxn], n, m;
vector<int>ps[maxn];
int main() {
cin>>n>>m;
for(int i=0;i<m;i++) {
cin>>st[i]>>p[i];
ps[st[i]].push_back(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;
}
# |
결과 |
실행 시간 |
메모리 |
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 |
564 KB |
Output is correct |
5 |
Correct |
2 ms |
676 KB |
Output is correct |
6 |
Correct |
2 ms |
676 KB |
Output is correct |
7 |
Correct |
2 ms |
676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
676 KB |
Output is correct |
2 |
Correct |
2 ms |
676 KB |
Output is correct |
3 |
Correct |
2 ms |
676 KB |
Output is correct |
4 |
Correct |
3 ms |
676 KB |
Output is correct |
5 |
Correct |
2 ms |
748 KB |
Output is correct |
6 |
Correct |
2 ms |
748 KB |
Output is correct |
7 |
Correct |
3 ms |
748 KB |
Output is correct |
8 |
Correct |
2 ms |
892 KB |
Output is correct |
9 |
Correct |
2 ms |
1020 KB |
Output is correct |
10 |
Correct |
2 ms |
1276 KB |
Output is correct |
11 |
Correct |
3 ms |
1276 KB |
Output is correct |
12 |
Correct |
3 ms |
1276 KB |
Output is correct |
13 |
Correct |
3 ms |
1276 KB |
Output is correct |
14 |
Correct |
4 ms |
1276 KB |
Output is correct |
15 |
Correct |
4 ms |
1276 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1276 KB |
Output is correct |
2 |
Correct |
2 ms |
1276 KB |
Output is correct |
3 |
Correct |
2 ms |
1276 KB |
Output is correct |
4 |
Correct |
2 ms |
1276 KB |
Output is correct |
5 |
Correct |
2 ms |
1276 KB |
Output is correct |
6 |
Correct |
2 ms |
1276 KB |
Output is correct |
7 |
Correct |
2 ms |
1276 KB |
Output is correct |
8 |
Correct |
3 ms |
1276 KB |
Output is correct |
9 |
Correct |
3 ms |
1276 KB |
Output is correct |
10 |
Correct |
4 ms |
1276 KB |
Output is correct |
11 |
Correct |
4 ms |
1276 KB |
Output is correct |
12 |
Correct |
3 ms |
1276 KB |
Output is correct |
13 |
Correct |
3 ms |
1276 KB |
Output is correct |
14 |
Correct |
4 ms |
1276 KB |
Output is correct |
15 |
Correct |
4 ms |
1276 KB |
Output is correct |
16 |
Correct |
4 ms |
1276 KB |
Output is correct |
17 |
Correct |
9 ms |
6268 KB |
Output is correct |
18 |
Correct |
3 ms |
6268 KB |
Output is correct |
19 |
Correct |
3 ms |
6268 KB |
Output is correct |
20 |
Correct |
13 ms |
12796 KB |
Output is correct |
21 |
Correct |
3 ms |
12796 KB |
Output is correct |
22 |
Correct |
3 ms |
12796 KB |
Output is correct |
23 |
Correct |
14 ms |
12796 KB |
Output is correct |
24 |
Correct |
20 ms |
17032 KB |
Output is correct |
25 |
Correct |
16 ms |
18300 KB |
Output is correct |
26 |
Correct |
13 ms |
18300 KB |
Output is correct |
27 |
Correct |
17 ms |
18300 KB |
Output is correct |
28 |
Correct |
15 ms |
18300 KB |
Output is correct |
29 |
Correct |
28 ms |
18300 KB |
Output is correct |
30 |
Correct |
15 ms |
18300 KB |
Output is correct |
31 |
Correct |
16 ms |
18300 KB |
Output is correct |
32 |
Correct |
15 ms |
18300 KB |
Output is correct |
33 |
Correct |
19 ms |
18300 KB |
Output is correct |
34 |
Correct |
18 ms |
18300 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
18300 KB |
Output is correct |
2 |
Correct |
2 ms |
18300 KB |
Output is correct |
3 |
Correct |
2 ms |
18300 KB |
Output is correct |
4 |
Correct |
2 ms |
18300 KB |
Output is correct |
5 |
Correct |
3 ms |
18300 KB |
Output is correct |
6 |
Correct |
2 ms |
18300 KB |
Output is correct |
7 |
Correct |
2 ms |
18300 KB |
Output is correct |
8 |
Correct |
2 ms |
18300 KB |
Output is correct |
9 |
Correct |
2 ms |
18300 KB |
Output is correct |
10 |
Correct |
4 ms |
18300 KB |
Output is correct |
11 |
Correct |
6 ms |
18300 KB |
Output is correct |
12 |
Correct |
5 ms |
18300 KB |
Output is correct |
13 |
Correct |
4 ms |
18300 KB |
Output is correct |
14 |
Correct |
4 ms |
18300 KB |
Output is correct |
15 |
Correct |
4 ms |
18300 KB |
Output is correct |
16 |
Correct |
3 ms |
18300 KB |
Output is correct |
17 |
Correct |
12 ms |
18300 KB |
Output is correct |
18 |
Correct |
3 ms |
18300 KB |
Output is correct |
19 |
Correct |
2 ms |
18300 KB |
Output is correct |
20 |
Correct |
13 ms |
18300 KB |
Output is correct |
21 |
Correct |
3 ms |
18300 KB |
Output is correct |
22 |
Correct |
3 ms |
18300 KB |
Output is correct |
23 |
Correct |
11 ms |
18300 KB |
Output is correct |
24 |
Correct |
16 ms |
18300 KB |
Output is correct |
25 |
Correct |
16 ms |
18300 KB |
Output is correct |
26 |
Correct |
13 ms |
18300 KB |
Output is correct |
27 |
Correct |
14 ms |
18300 KB |
Output is correct |
28 |
Correct |
15 ms |
18300 KB |
Output is correct |
29 |
Correct |
13 ms |
18300 KB |
Output is correct |
30 |
Correct |
15 ms |
18300 KB |
Output is correct |
31 |
Correct |
14 ms |
18300 KB |
Output is correct |
32 |
Correct |
13 ms |
18300 KB |
Output is correct |
33 |
Correct |
17 ms |
18300 KB |
Output is correct |
34 |
Correct |
17 ms |
18300 KB |
Output is correct |
35 |
Incorrect |
46 ms |
18300 KB |
Output isn't correct |
36 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
18300 KB |
Output is correct |
2 |
Correct |
2 ms |
18300 KB |
Output is correct |
3 |
Correct |
2 ms |
18300 KB |
Output is correct |
4 |
Correct |
2 ms |
18300 KB |
Output is correct |
5 |
Correct |
2 ms |
18300 KB |
Output is correct |
6 |
Correct |
2 ms |
18300 KB |
Output is correct |
7 |
Correct |
2 ms |
18300 KB |
Output is correct |
8 |
Correct |
3 ms |
18300 KB |
Output is correct |
9 |
Correct |
3 ms |
18300 KB |
Output is correct |
10 |
Correct |
3 ms |
18300 KB |
Output is correct |
11 |
Correct |
4 ms |
18300 KB |
Output is correct |
12 |
Correct |
4 ms |
18300 KB |
Output is correct |
13 |
Correct |
3 ms |
18300 KB |
Output is correct |
14 |
Correct |
4 ms |
18300 KB |
Output is correct |
15 |
Correct |
4 ms |
18300 KB |
Output is correct |
16 |
Correct |
3 ms |
18300 KB |
Output is correct |
17 |
Correct |
10 ms |
18300 KB |
Output is correct |
18 |
Correct |
3 ms |
18300 KB |
Output is correct |
19 |
Correct |
3 ms |
18300 KB |
Output is correct |
20 |
Correct |
17 ms |
18300 KB |
Output is correct |
21 |
Correct |
3 ms |
18300 KB |
Output is correct |
22 |
Correct |
3 ms |
18300 KB |
Output is correct |
23 |
Correct |
13 ms |
18300 KB |
Output is correct |
24 |
Correct |
18 ms |
18300 KB |
Output is correct |
25 |
Correct |
18 ms |
18304 KB |
Output is correct |
26 |
Correct |
15 ms |
18304 KB |
Output is correct |
27 |
Correct |
15 ms |
18304 KB |
Output is correct |
28 |
Correct |
17 ms |
18304 KB |
Output is correct |
29 |
Correct |
18 ms |
18304 KB |
Output is correct |
30 |
Correct |
13 ms |
18304 KB |
Output is correct |
31 |
Correct |
15 ms |
18304 KB |
Output is correct |
32 |
Correct |
15 ms |
18304 KB |
Output is correct |
33 |
Correct |
19 ms |
18304 KB |
Output is correct |
34 |
Correct |
18 ms |
18304 KB |
Output is correct |
35 |
Incorrect |
52 ms |
18304 KB |
Output isn't correct |
36 |
Halted |
0 ms |
0 KB |
- |