#include <cstdio>
#include <cstdlib>
#include <vector>
#include <algorithm>
#include <utility>
#include <queue>
using namespace std;
typedef pair<int,int> pi;
const int B = 150;
int m, n;
bool r_dir[30005][B], l_dir[30005][B];
bool vis[30005 * B];
vector<pi> graph[30005 * B];
priority_queue<pi,vector<pi>,greater<pi> > pq;
int dijkstra(int st, int ed){
pq.push(pi(0,st));
while (!pq.empty()) {
pi t = pq.top();
pq.pop();
if(t.second == ed){
return t.first;
}
if(vis[t.second]) continue;
vis[t.second] = 1;
for (auto &i : graph[t.second]){
if(!vis[i.second]) pq.push(pi(i.first + t.first,i.second));
}
}
return -1;
}
int main(){
int st = 0, ed = 0;
scanf("%d %d",&n,&m);
for (int i=0; i<m; i++) {
int x,y;
scanf("%d %d",&x,&y);
if(i == 0){
st = x;
}
else if(i == 1){
ed = x;
}
if(y < B){
r_dir[x][y] = 1;
l_dir[x][y] = 1;
graph[x].push_back(pi(0,x + n * y));
}
else{
for (int j=1; x - j * y >= 0; j++) {
graph[x].push_back(pi(j,x - j * y));
}
for (int j=1; x + j * y < n; j++) {
graph[x].push_back(pi(j,x + j * y));
}
}
}
for (int i=1; i<B; i++) {
for (int j=i; j<n; j++) {
r_dir[j][i] |= r_dir[j - i][i];
}
for (int j=n-i-1; j>=0; j--) {
l_dir[j][i] |= l_dir[j + i][i];
}
for (int j=0; j<n; j++) {
if(r_dir[j][i] && j + i < n){
graph[i * n + j].push_back(pi(1,i * n + j + i));
}
if(l_dir[j][i] && j - i >= 0){
graph[i * n + j].push_back(pi(1,i * n + j - i));
}
graph[i * n + j].push_back(pi(0,j));
}
}
printf("%d",dijkstra(st,ed));
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
119884 KB |
Output is correct |
2 |
Correct |
22 ms |
119884 KB |
Output is correct |
3 |
Correct |
20 ms |
119884 KB |
Output is correct |
4 |
Correct |
24 ms |
119884 KB |
Output is correct |
5 |
Correct |
17 ms |
119884 KB |
Output is correct |
6 |
Correct |
18 ms |
119884 KB |
Output is correct |
7 |
Correct |
34 ms |
119884 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
119884 KB |
Output is correct |
2 |
Correct |
22 ms |
119884 KB |
Output is correct |
3 |
Correct |
34 ms |
119884 KB |
Output is correct |
4 |
Correct |
22 ms |
119884 KB |
Output is correct |
5 |
Correct |
18 ms |
119884 KB |
Output is correct |
6 |
Correct |
14 ms |
119884 KB |
Output is correct |
7 |
Correct |
29 ms |
119884 KB |
Output is correct |
8 |
Correct |
18 ms |
120016 KB |
Output is correct |
9 |
Correct |
19 ms |
120148 KB |
Output is correct |
10 |
Correct |
18 ms |
120280 KB |
Output is correct |
11 |
Correct |
27 ms |
120280 KB |
Output is correct |
12 |
Correct |
11 ms |
120280 KB |
Output is correct |
13 |
Correct |
15 ms |
120280 KB |
Output is correct |
14 |
Correct |
16 ms |
120280 KB |
Output is correct |
15 |
Correct |
9 ms |
120280 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
119884 KB |
Output is correct |
2 |
Correct |
11 ms |
119884 KB |
Output is correct |
3 |
Correct |
9 ms |
119884 KB |
Output is correct |
4 |
Correct |
12 ms |
119884 KB |
Output is correct |
5 |
Correct |
17 ms |
119884 KB |
Output is correct |
6 |
Correct |
8 ms |
119884 KB |
Output is correct |
7 |
Correct |
14 ms |
119884 KB |
Output is correct |
8 |
Correct |
14 ms |
120016 KB |
Output is correct |
9 |
Correct |
15 ms |
120148 KB |
Output is correct |
10 |
Correct |
22 ms |
120280 KB |
Output is correct |
11 |
Correct |
17 ms |
120280 KB |
Output is correct |
12 |
Correct |
15 ms |
120280 KB |
Output is correct |
13 |
Correct |
15 ms |
120280 KB |
Output is correct |
14 |
Correct |
24 ms |
120280 KB |
Output is correct |
15 |
Correct |
15 ms |
120280 KB |
Output is correct |
16 |
Correct |
12 ms |
120808 KB |
Output is correct |
17 |
Correct |
23 ms |
123316 KB |
Output is correct |
18 |
Correct |
34 ms |
127936 KB |
Output is correct |
19 |
Correct |
44 ms |
129124 KB |
Output is correct |
20 |
Correct |
53 ms |
129256 KB |
Output is correct |
21 |
Correct |
27 ms |
121600 KB |
Output is correct |
22 |
Correct |
31 ms |
128068 KB |
Output is correct |
23 |
Correct |
37 ms |
127276 KB |
Output is correct |
24 |
Correct |
42 ms |
128728 KB |
Output is correct |
25 |
Correct |
40 ms |
129256 KB |
Output is correct |
26 |
Correct |
20 ms |
129124 KB |
Output is correct |
27 |
Correct |
34 ms |
129124 KB |
Output is correct |
28 |
Correct |
45 ms |
129124 KB |
Output is correct |
29 |
Correct |
54 ms |
129124 KB |
Output is correct |
30 |
Correct |
37 ms |
129124 KB |
Output is correct |
31 |
Correct |
46 ms |
129124 KB |
Output is correct |
32 |
Correct |
32 ms |
129124 KB |
Output is correct |
33 |
Correct |
65 ms |
129256 KB |
Output is correct |
34 |
Correct |
60 ms |
129256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
119884 KB |
Output is correct |
2 |
Correct |
11 ms |
119884 KB |
Output is correct |
3 |
Correct |
12 ms |
119884 KB |
Output is correct |
4 |
Correct |
17 ms |
119884 KB |
Output is correct |
5 |
Correct |
18 ms |
119884 KB |
Output is correct |
6 |
Correct |
12 ms |
119884 KB |
Output is correct |
7 |
Correct |
16 ms |
119884 KB |
Output is correct |
8 |
Correct |
19 ms |
120016 KB |
Output is correct |
9 |
Correct |
19 ms |
120148 KB |
Output is correct |
10 |
Correct |
19 ms |
120280 KB |
Output is correct |
11 |
Correct |
31 ms |
120280 KB |
Output is correct |
12 |
Correct |
11 ms |
120280 KB |
Output is correct |
13 |
Correct |
9 ms |
120280 KB |
Output is correct |
14 |
Correct |
18 ms |
120280 KB |
Output is correct |
15 |
Correct |
19 ms |
120280 KB |
Output is correct |
16 |
Correct |
16 ms |
120808 KB |
Output is correct |
17 |
Correct |
37 ms |
123316 KB |
Output is correct |
18 |
Correct |
35 ms |
127936 KB |
Output is correct |
19 |
Correct |
34 ms |
129124 KB |
Output is correct |
20 |
Correct |
50 ms |
129256 KB |
Output is correct |
21 |
Correct |
26 ms |
121600 KB |
Output is correct |
22 |
Correct |
34 ms |
128068 KB |
Output is correct |
23 |
Correct |
27 ms |
127276 KB |
Output is correct |
24 |
Correct |
31 ms |
128728 KB |
Output is correct |
25 |
Correct |
40 ms |
129256 KB |
Output is correct |
26 |
Correct |
42 ms |
129124 KB |
Output is correct |
27 |
Correct |
40 ms |
129124 KB |
Output is correct |
28 |
Correct |
36 ms |
129124 KB |
Output is correct |
29 |
Correct |
52 ms |
129124 KB |
Output is correct |
30 |
Correct |
42 ms |
129124 KB |
Output is correct |
31 |
Correct |
43 ms |
129124 KB |
Output is correct |
32 |
Correct |
47 ms |
129124 KB |
Output is correct |
33 |
Correct |
64 ms |
129256 KB |
Output is correct |
34 |
Correct |
41 ms |
129256 KB |
Output is correct |
35 |
Correct |
37 ms |
127180 KB |
Output is correct |
36 |
Correct |
38 ms |
125032 KB |
Output is correct |
37 |
Correct |
45 ms |
130304 KB |
Output is correct |
38 |
Correct |
48 ms |
130184 KB |
Output is correct |
39 |
Correct |
58 ms |
130048 KB |
Output is correct |
40 |
Correct |
55 ms |
130048 KB |
Output is correct |
41 |
Correct |
54 ms |
130216 KB |
Output is correct |
42 |
Correct |
46 ms |
129392 KB |
Output is correct |
43 |
Correct |
55 ms |
129392 KB |
Output is correct |
44 |
Correct |
27 ms |
129516 KB |
Output is correct |
45 |
Correct |
167 ms |
134276 KB |
Output is correct |
46 |
Correct |
129 ms |
134280 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
119884 KB |
Output is correct |
2 |
Correct |
29 ms |
119884 KB |
Output is correct |
3 |
Correct |
17 ms |
119884 KB |
Output is correct |
4 |
Correct |
12 ms |
119884 KB |
Output is correct |
5 |
Correct |
16 ms |
119884 KB |
Output is correct |
6 |
Correct |
21 ms |
119884 KB |
Output is correct |
7 |
Correct |
16 ms |
119884 KB |
Output is correct |
8 |
Correct |
14 ms |
120016 KB |
Output is correct |
9 |
Correct |
22 ms |
120148 KB |
Output is correct |
10 |
Correct |
21 ms |
120280 KB |
Output is correct |
11 |
Correct |
11 ms |
120280 KB |
Output is correct |
12 |
Correct |
11 ms |
120280 KB |
Output is correct |
13 |
Correct |
23 ms |
120280 KB |
Output is correct |
14 |
Correct |
24 ms |
120280 KB |
Output is correct |
15 |
Correct |
32 ms |
120280 KB |
Output is correct |
16 |
Correct |
21 ms |
120808 KB |
Output is correct |
17 |
Correct |
32 ms |
123316 KB |
Output is correct |
18 |
Correct |
34 ms |
127936 KB |
Output is correct |
19 |
Correct |
29 ms |
129124 KB |
Output is correct |
20 |
Correct |
43 ms |
129256 KB |
Output is correct |
21 |
Correct |
23 ms |
121600 KB |
Output is correct |
22 |
Correct |
43 ms |
128068 KB |
Output is correct |
23 |
Correct |
35 ms |
127276 KB |
Output is correct |
24 |
Correct |
37 ms |
128728 KB |
Output is correct |
25 |
Correct |
41 ms |
129256 KB |
Output is correct |
26 |
Correct |
43 ms |
129124 KB |
Output is correct |
27 |
Correct |
58 ms |
129124 KB |
Output is correct |
28 |
Correct |
46 ms |
129124 KB |
Output is correct |
29 |
Correct |
54 ms |
129124 KB |
Output is correct |
30 |
Correct |
40 ms |
129124 KB |
Output is correct |
31 |
Correct |
43 ms |
129124 KB |
Output is correct |
32 |
Correct |
45 ms |
129124 KB |
Output is correct |
33 |
Correct |
62 ms |
129256 KB |
Output is correct |
34 |
Correct |
46 ms |
129256 KB |
Output is correct |
35 |
Correct |
46 ms |
127180 KB |
Output is correct |
36 |
Correct |
30 ms |
125032 KB |
Output is correct |
37 |
Correct |
60 ms |
130304 KB |
Output is correct |
38 |
Correct |
62 ms |
130184 KB |
Output is correct |
39 |
Correct |
43 ms |
130048 KB |
Output is correct |
40 |
Correct |
61 ms |
130048 KB |
Output is correct |
41 |
Correct |
62 ms |
130216 KB |
Output is correct |
42 |
Correct |
37 ms |
129392 KB |
Output is correct |
43 |
Correct |
44 ms |
129392 KB |
Output is correct |
44 |
Correct |
51 ms |
129516 KB |
Output is correct |
45 |
Correct |
173 ms |
134276 KB |
Output is correct |
46 |
Correct |
124 ms |
134280 KB |
Output is correct |
47 |
Correct |
236 ms |
183576 KB |
Output is correct |
48 |
Correct |
233 ms |
229180 KB |
Output is correct |
49 |
Correct |
307 ms |
238948 KB |
Output is correct |
50 |
Correct |
342 ms |
250564 KB |
Output is correct |
51 |
Memory limit exceeded |
278 ms |
262144 KB |
Memory limit exceeded |
52 |
Halted |
0 ms |
0 KB |
- |