#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];
queue<int> pq[30005];
int dijkstra(int st, int ed){
pq[0].push(st);
int dist = 0, cnt = 1;
while (cnt) {
while (!pq[dist%n].empty()) {
int t = pq[dist%n].front();
pq[dist%n].pop();
cnt--;
if(t == ed){
return dist;
}
if(vis[t]) continue;
vis[t] = 1;
if(t < n){
for (auto &i : graph[t]){
if(!vis[i.second])
pq[(i.first + dist)%n].push(i.second), cnt++;
}
}
int px = t % n;
int py = t / n;
if(r_dir[px][py] && px + py < n){
if(!vis[py * n + px + py]){
cnt++;
pq[(dist + 1)%n].push(py * n + px + py);
}
}
if(l_dir[px][py] && px - py >= 0){
if(!vis[py * n + px - py]){
cnt++;
pq[(dist + 1)%n].push(py * n + px - py);
}
}
if(py){
if(!vis[px]){
cnt++;
pq[dist%n].push(px);
}
}
}
dist++;
}
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];
}
}
printf("%d",dijkstra(st,ed));
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
35508 KB |
Output is correct |
2 |
Correct |
0 ms |
35508 KB |
Output is correct |
3 |
Correct |
4 ms |
35508 KB |
Output is correct |
4 |
Correct |
0 ms |
35508 KB |
Output is correct |
5 |
Correct |
10 ms |
35508 KB |
Output is correct |
6 |
Correct |
14 ms |
35508 KB |
Output is correct |
7 |
Correct |
6 ms |
35508 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
35508 KB |
Output is correct |
2 |
Correct |
0 ms |
35508 KB |
Output is correct |
3 |
Correct |
24 ms |
35508 KB |
Output is correct |
4 |
Correct |
12 ms |
35508 KB |
Output is correct |
5 |
Correct |
8 ms |
35508 KB |
Output is correct |
6 |
Correct |
7 ms |
35508 KB |
Output is correct |
7 |
Correct |
11 ms |
35508 KB |
Output is correct |
8 |
Correct |
17 ms |
35508 KB |
Output is correct |
9 |
Correct |
8 ms |
35508 KB |
Output is correct |
10 |
Correct |
20 ms |
35508 KB |
Output is correct |
11 |
Correct |
15 ms |
35640 KB |
Output is correct |
12 |
Correct |
8 ms |
35644 KB |
Output is correct |
13 |
Correct |
18 ms |
35644 KB |
Output is correct |
14 |
Correct |
17 ms |
35640 KB |
Output is correct |
15 |
Correct |
17 ms |
35640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
35508 KB |
Output is correct |
2 |
Correct |
12 ms |
35508 KB |
Output is correct |
3 |
Correct |
25 ms |
35508 KB |
Output is correct |
4 |
Correct |
8 ms |
35508 KB |
Output is correct |
5 |
Correct |
13 ms |
35508 KB |
Output is correct |
6 |
Correct |
12 ms |
35508 KB |
Output is correct |
7 |
Correct |
4 ms |
35508 KB |
Output is correct |
8 |
Correct |
0 ms |
35508 KB |
Output is correct |
9 |
Correct |
25 ms |
35508 KB |
Output is correct |
10 |
Correct |
25 ms |
35508 KB |
Output is correct |
11 |
Correct |
17 ms |
35640 KB |
Output is correct |
12 |
Correct |
5 ms |
35644 KB |
Output is correct |
13 |
Correct |
15 ms |
35644 KB |
Output is correct |
14 |
Correct |
13 ms |
35640 KB |
Output is correct |
15 |
Correct |
16 ms |
35640 KB |
Output is correct |
16 |
Correct |
13 ms |
35640 KB |
Output is correct |
17 |
Correct |
18 ms |
35640 KB |
Output is correct |
18 |
Correct |
5 ms |
35640 KB |
Output is correct |
19 |
Correct |
13 ms |
35640 KB |
Output is correct |
20 |
Correct |
7 ms |
35640 KB |
Output is correct |
21 |
Correct |
13 ms |
35508 KB |
Output is correct |
22 |
Correct |
3 ms |
35640 KB |
Output is correct |
23 |
Correct |
29 ms |
35640 KB |
Output is correct |
24 |
Correct |
17 ms |
35640 KB |
Output is correct |
25 |
Correct |
18 ms |
35640 KB |
Output is correct |
26 |
Correct |
12 ms |
35640 KB |
Output is correct |
27 |
Correct |
20 ms |
35640 KB |
Output is correct |
28 |
Correct |
26 ms |
35640 KB |
Output is correct |
29 |
Correct |
22 ms |
35640 KB |
Output is correct |
30 |
Correct |
18 ms |
35508 KB |
Output is correct |
31 |
Correct |
23 ms |
35508 KB |
Output is correct |
32 |
Correct |
16 ms |
35640 KB |
Output is correct |
33 |
Correct |
16 ms |
35640 KB |
Output is correct |
34 |
Correct |
23 ms |
35640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
35508 KB |
Output is correct |
2 |
Correct |
24 ms |
35508 KB |
Output is correct |
3 |
Correct |
10 ms |
35508 KB |
Output is correct |
4 |
Correct |
20 ms |
35508 KB |
Output is correct |
5 |
Correct |
0 ms |
35508 KB |
Output is correct |
6 |
Correct |
12 ms |
35508 KB |
Output is correct |
7 |
Correct |
16 ms |
35508 KB |
Output is correct |
8 |
Correct |
8 ms |
35508 KB |
Output is correct |
9 |
Correct |
23 ms |
35508 KB |
Output is correct |
10 |
Correct |
0 ms |
35508 KB |
Output is correct |
11 |
Correct |
11 ms |
35640 KB |
Output is correct |
12 |
Correct |
17 ms |
35644 KB |
Output is correct |
13 |
Correct |
11 ms |
35644 KB |
Output is correct |
14 |
Correct |
13 ms |
35640 KB |
Output is correct |
15 |
Correct |
11 ms |
35640 KB |
Output is correct |
16 |
Correct |
21 ms |
35640 KB |
Output is correct |
17 |
Correct |
11 ms |
35640 KB |
Output is correct |
18 |
Correct |
11 ms |
35640 KB |
Output is correct |
19 |
Correct |
15 ms |
35640 KB |
Output is correct |
20 |
Correct |
9 ms |
35640 KB |
Output is correct |
21 |
Correct |
16 ms |
35508 KB |
Output is correct |
22 |
Correct |
15 ms |
35640 KB |
Output is correct |
23 |
Correct |
14 ms |
35640 KB |
Output is correct |
24 |
Correct |
19 ms |
35640 KB |
Output is correct |
25 |
Correct |
21 ms |
35640 KB |
Output is correct |
26 |
Correct |
18 ms |
35640 KB |
Output is correct |
27 |
Correct |
14 ms |
35640 KB |
Output is correct |
28 |
Correct |
17 ms |
35640 KB |
Output is correct |
29 |
Correct |
20 ms |
35640 KB |
Output is correct |
30 |
Correct |
20 ms |
35508 KB |
Output is correct |
31 |
Correct |
16 ms |
35508 KB |
Output is correct |
32 |
Correct |
18 ms |
35640 KB |
Output is correct |
33 |
Correct |
12 ms |
35640 KB |
Output is correct |
34 |
Correct |
18 ms |
35640 KB |
Output is correct |
35 |
Correct |
24 ms |
36036 KB |
Output is correct |
36 |
Correct |
14 ms |
35640 KB |
Output is correct |
37 |
Correct |
11 ms |
36432 KB |
Output is correct |
38 |
Correct |
20 ms |
36300 KB |
Output is correct |
39 |
Correct |
26 ms |
36300 KB |
Output is correct |
40 |
Correct |
20 ms |
36300 KB |
Output is correct |
41 |
Correct |
24 ms |
36300 KB |
Output is correct |
42 |
Correct |
14 ms |
36024 KB |
Output is correct |
43 |
Correct |
18 ms |
36024 KB |
Output is correct |
44 |
Correct |
11 ms |
36032 KB |
Output is correct |
45 |
Correct |
44 ms |
38340 KB |
Output is correct |
46 |
Correct |
41 ms |
38336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
35508 KB |
Output is correct |
2 |
Correct |
8 ms |
35508 KB |
Output is correct |
3 |
Correct |
12 ms |
35508 KB |
Output is correct |
4 |
Correct |
12 ms |
35508 KB |
Output is correct |
5 |
Correct |
16 ms |
35508 KB |
Output is correct |
6 |
Correct |
10 ms |
35508 KB |
Output is correct |
7 |
Correct |
13 ms |
35508 KB |
Output is correct |
8 |
Correct |
6 ms |
35508 KB |
Output is correct |
9 |
Correct |
19 ms |
35508 KB |
Output is correct |
10 |
Correct |
16 ms |
35508 KB |
Output is correct |
11 |
Correct |
21 ms |
35640 KB |
Output is correct |
12 |
Correct |
6 ms |
35644 KB |
Output is correct |
13 |
Correct |
6 ms |
35644 KB |
Output is correct |
14 |
Correct |
9 ms |
35640 KB |
Output is correct |
15 |
Correct |
8 ms |
35640 KB |
Output is correct |
16 |
Correct |
15 ms |
35640 KB |
Output is correct |
17 |
Correct |
18 ms |
35640 KB |
Output is correct |
18 |
Correct |
18 ms |
35640 KB |
Output is correct |
19 |
Correct |
21 ms |
35640 KB |
Output is correct |
20 |
Correct |
11 ms |
35640 KB |
Output is correct |
21 |
Correct |
18 ms |
35508 KB |
Output is correct |
22 |
Correct |
13 ms |
35640 KB |
Output is correct |
23 |
Correct |
14 ms |
35640 KB |
Output is correct |
24 |
Correct |
10 ms |
35640 KB |
Output is correct |
25 |
Correct |
21 ms |
35640 KB |
Output is correct |
26 |
Correct |
17 ms |
35640 KB |
Output is correct |
27 |
Correct |
7 ms |
35640 KB |
Output is correct |
28 |
Correct |
8 ms |
35640 KB |
Output is correct |
29 |
Correct |
11 ms |
35640 KB |
Output is correct |
30 |
Correct |
24 ms |
35508 KB |
Output is correct |
31 |
Correct |
14 ms |
35508 KB |
Output is correct |
32 |
Correct |
22 ms |
35640 KB |
Output is correct |
33 |
Correct |
22 ms |
35640 KB |
Output is correct |
34 |
Correct |
26 ms |
35640 KB |
Output is correct |
35 |
Correct |
25 ms |
36036 KB |
Output is correct |
36 |
Correct |
14 ms |
35640 KB |
Output is correct |
37 |
Correct |
10 ms |
36432 KB |
Output is correct |
38 |
Correct |
16 ms |
36300 KB |
Output is correct |
39 |
Correct |
25 ms |
36300 KB |
Output is correct |
40 |
Correct |
19 ms |
36300 KB |
Output is correct |
41 |
Correct |
11 ms |
36300 KB |
Output is correct |
42 |
Correct |
36 ms |
36024 KB |
Output is correct |
43 |
Correct |
21 ms |
36024 KB |
Output is correct |
44 |
Correct |
22 ms |
36032 KB |
Output is correct |
45 |
Correct |
45 ms |
38340 KB |
Output is correct |
46 |
Correct |
27 ms |
38336 KB |
Output is correct |
47 |
Correct |
48 ms |
39864 KB |
Output is correct |
48 |
Correct |
59 ms |
36696 KB |
Output is correct |
49 |
Correct |
46 ms |
36564 KB |
Output is correct |
50 |
Correct |
48 ms |
36432 KB |
Output is correct |
51 |
Correct |
81 ms |
37356 KB |
Output is correct |
52 |
Correct |
70 ms |
37356 KB |
Output is correct |
53 |
Correct |
61 ms |
35640 KB |
Output is correct |
54 |
Correct |
57 ms |
35508 KB |
Output is correct |
55 |
Correct |
62 ms |
35508 KB |
Output is correct |
56 |
Correct |
65 ms |
36564 KB |
Output is correct |
57 |
Correct |
51 ms |
35640 KB |
Output is correct |
58 |
Correct |
60 ms |
36016 KB |
Output is correct |
59 |
Correct |
57 ms |
36016 KB |
Output is correct |
60 |
Correct |
64 ms |
36380 KB |
Output is correct |
61 |
Correct |
69 ms |
36216 KB |
Output is correct |
62 |
Correct |
95 ms |
38016 KB |
Output is correct |
63 |
Correct |
146 ms |
63320 KB |
Output is correct |
64 |
Correct |
145 ms |
74508 KB |
Output is correct |
65 |
Correct |
191 ms |
75924 KB |
Output is correct |
66 |
Correct |
338 ms |
74144 KB |
Output is correct |
67 |
Correct |
238 ms |
74124 KB |
Output is correct |