#include<bits/stdc++.h>
using namespace std;
const int inf = 1e9;
int main()
{
int n, m;
cin >> n >> m;
int dist[n][m];
for(int i = 0; i < n; i ++)
for(int j = 0; j < m ; j++)
dist[i][j] = inf;
vector<int> L[n];
int p[m], b[m];
for(int i = 0; i < m; i ++)
{
cin >> b[i] >> p[i];
L[b[i]].push_back(i);
}
deque<pair<int,int> > Q;
Q.push_back({b[0], 0});
dist[b[0]][0] = 0;
while(Q.size())
{
int f = Q.front().first, s = Q.front().second;
Q.pop_front();
// cerr << f << ' ' << s << ' ' << dist[f][s] << endl;
for(int res : L[f])
if(dist[f][s] < dist[f][res])
{
dist[f][res] = dist[f][s];
Q.push_front({f, res});
}
// L[f].clear();
if(f + p[s] < n && dist[f][s] + 1 < dist[f + p[s]][s])
{
dist[f + p[s]][s] = dist[f][s] + 1;
Q.push_front({f + p[s], s});
}
if(f - p[s] >= 0 && dist[f][s] + 1 < dist[f - p[s]][s])
{
dist[f - p[s]][s] = dist[f][s] + 1;
Q.push_front({f - p[s], s});
}
}
if(dist[b[1]][1] == inf)
cout << -1 << endl;
else
cout << dist[b[1]][1] << endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
440 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
436 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
2 ms |
604 KB |
Output is correct |
11 |
Correct |
24 ms |
1116 KB |
Output is correct |
12 |
Correct |
8 ms |
1116 KB |
Output is correct |
13 |
Correct |
8 ms |
1116 KB |
Output is correct |
14 |
Correct |
3 ms |
1116 KB |
Output is correct |
15 |
Correct |
3 ms |
1160 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
2 ms |
604 KB |
Output is correct |
11 |
Correct |
24 ms |
1276 KB |
Output is correct |
12 |
Correct |
7 ms |
1276 KB |
Output is correct |
13 |
Correct |
8 ms |
1116 KB |
Output is correct |
14 |
Correct |
3 ms |
1116 KB |
Output is correct |
15 |
Correct |
3 ms |
1116 KB |
Output is correct |
16 |
Correct |
1 ms |
1116 KB |
Output is correct |
17 |
Correct |
177 ms |
5464 KB |
Output is correct |
18 |
Correct |
5 ms |
7256 KB |
Output is correct |
19 |
Correct |
3 ms |
4700 KB |
Output is correct |
20 |
Correct |
152 ms |
16220 KB |
Output is correct |
21 |
Correct |
2 ms |
2136 KB |
Output is correct |
22 |
Correct |
3 ms |
5348 KB |
Output is correct |
23 |
Correct |
8 ms |
6492 KB |
Output is correct |
24 |
Correct |
76 ms |
14568 KB |
Output is correct |
25 |
Correct |
15 ms |
16220 KB |
Output is correct |
26 |
Correct |
141 ms |
16172 KB |
Output is correct |
27 |
Correct |
75 ms |
15960 KB |
Output is correct |
28 |
Correct |
11 ms |
16096 KB |
Output is correct |
29 |
Correct |
10 ms |
6236 KB |
Output is correct |
30 |
Correct |
2 ms |
1884 KB |
Output is correct |
31 |
Correct |
4 ms |
4956 KB |
Output is correct |
32 |
Correct |
3 ms |
4188 KB |
Output is correct |
33 |
Correct |
35 ms |
15996 KB |
Output is correct |
34 |
Correct |
36 ms |
15964 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
432 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
2 ms |
604 KB |
Output is correct |
11 |
Correct |
25 ms |
1116 KB |
Output is correct |
12 |
Correct |
8 ms |
1268 KB |
Output is correct |
13 |
Correct |
7 ms |
1092 KB |
Output is correct |
14 |
Correct |
4 ms |
1276 KB |
Output is correct |
15 |
Correct |
3 ms |
1116 KB |
Output is correct |
16 |
Correct |
1 ms |
1116 KB |
Output is correct |
17 |
Correct |
178 ms |
5656 KB |
Output is correct |
18 |
Correct |
4 ms |
7256 KB |
Output is correct |
19 |
Correct |
3 ms |
4616 KB |
Output is correct |
20 |
Correct |
146 ms |
16220 KB |
Output is correct |
21 |
Correct |
2 ms |
2140 KB |
Output is correct |
22 |
Correct |
3 ms |
5468 KB |
Output is correct |
23 |
Correct |
8 ms |
6492 KB |
Output is correct |
24 |
Correct |
77 ms |
14424 KB |
Output is correct |
25 |
Correct |
15 ms |
15960 KB |
Output is correct |
26 |
Correct |
151 ms |
16172 KB |
Output is correct |
27 |
Correct |
75 ms |
15964 KB |
Output is correct |
28 |
Correct |
12 ms |
16220 KB |
Output is correct |
29 |
Correct |
10 ms |
6236 KB |
Output is correct |
30 |
Correct |
2 ms |
1716 KB |
Output is correct |
31 |
Correct |
4 ms |
4956 KB |
Output is correct |
32 |
Correct |
3 ms |
4188 KB |
Output is correct |
33 |
Correct |
35 ms |
15972 KB |
Output is correct |
34 |
Correct |
39 ms |
15960 KB |
Output is correct |
35 |
Execution timed out |
1025 ms |
145744 KB |
Time limit exceeded |
36 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
600 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
2 ms |
604 KB |
Output is correct |
11 |
Correct |
24 ms |
1280 KB |
Output is correct |
12 |
Correct |
7 ms |
1116 KB |
Output is correct |
13 |
Correct |
8 ms |
1112 KB |
Output is correct |
14 |
Correct |
3 ms |
1116 KB |
Output is correct |
15 |
Correct |
3 ms |
1112 KB |
Output is correct |
16 |
Correct |
1 ms |
1116 KB |
Output is correct |
17 |
Correct |
193 ms |
5648 KB |
Output is correct |
18 |
Correct |
5 ms |
7260 KB |
Output is correct |
19 |
Correct |
3 ms |
4700 KB |
Output is correct |
20 |
Correct |
153 ms |
16220 KB |
Output is correct |
21 |
Correct |
2 ms |
2140 KB |
Output is correct |
22 |
Correct |
5 ms |
5468 KB |
Output is correct |
23 |
Correct |
8 ms |
6492 KB |
Output is correct |
24 |
Correct |
76 ms |
14428 KB |
Output is correct |
25 |
Correct |
14 ms |
16220 KB |
Output is correct |
26 |
Correct |
148 ms |
16172 KB |
Output is correct |
27 |
Correct |
75 ms |
15964 KB |
Output is correct |
28 |
Correct |
10 ms |
16220 KB |
Output is correct |
29 |
Correct |
10 ms |
6340 KB |
Output is correct |
30 |
Correct |
2 ms |
2136 KB |
Output is correct |
31 |
Correct |
5 ms |
4956 KB |
Output is correct |
32 |
Correct |
3 ms |
4228 KB |
Output is correct |
33 |
Correct |
35 ms |
16068 KB |
Output is correct |
34 |
Correct |
36 ms |
15960 KB |
Output is correct |
35 |
Execution timed out |
1071 ms |
145600 KB |
Time limit exceeded |
36 |
Halted |
0 ms |
0 KB |
- |