#include <cstdio>
#include <map>
#include <vector>
#include <algorithm>
#include <utility>
#include <queue>
using namespace std;
typedef pair<int,int> pi;
int m, n;
vector<int> vertex;
vector<int> dummy[30005];
vector<int> dx[30005];
bool r_dir[30005][180], l_dir[30005][180];
vector<vector<int> > graph0, graph1;
vector<bool> vis;
queue<int> pq[2];
inline int oldpi(int i, int j){
return n * j + i;
}
inline int oldpi_first(int p){
return (p % n + n) % n;
}
inline int oldpi_second(int p){
return (p - oldpi_first(p)) / n;
}
int dijkstra(int st, int ed){
pq[0].push(st);
int dist = 0;
while (1) {
if(pq[dist%2].empty()) break;
while (!pq[dist%2].empty()) {
int qf = pq[dist%2].front();
pq[dist%2].pop();
if(qf == ed){
return dist;
}
if(vis[qf]) continue;
vis[qf] = 1;
for (auto &i : graph0[qf]){
if(!vis[i]){
pq[dist%2].push(i);
}
}
for (auto &i : graph1[qf]){
if(!vis[i]){
pq[(dist+1)%2].push(i);
}
}
}
dist++;
}
return -1;
}
int main(){
int st, ed;
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 < 180){
r_dir[x][y] = 1;
l_dir[x][y] = 1;
}
else{
for (int j=0; j * y + x < n; j++) {
dx[j * y + x].push_back(y);
}
for (int j=0; j * y + x >= 0; j--) {
dx[j * y + x].push_back(-y);
}
}
dummy[x].push_back(y);
}
for (int i=1; i<180; 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]){
dx[j].push_back(i);
}
if(l_dir[j][i]){
dx[j].push_back(-i);
}
}
}
for (int i=0; i<n; i++) {
sort(dx[i].begin(),dx[i].end());
dx[i].resize(unique(dx[i].begin(),dx[i].end()) - dx[i].begin());
for (auto &j : dx[i]){
vertex.push_back(oldpi(i,j));
}
}
sort(vertex.begin(),vertex.end());
graph0.resize(n + vertex.size());
graph1.resize(n + vertex.size());
vis.resize(n + vertex.size());
for (int i=0; i<n; i++) {
for(auto &j : dummy[i]){
int pos = (int)(lower_bound(vertex.begin(),vertex.end(),oldpi(i,j)) - vertex.begin());
if(pos != vertex.size() && vertex[pos] == oldpi(i,j)){
graph0[i].push_back(pos + n);
}
pos = (int)(lower_bound(vertex.begin(),vertex.end(),oldpi(i,-j)) - vertex.begin());
if(pos != vertex.size() && vertex[pos] == oldpi(i,-j)){
graph0[i].push_back(pos + n);
}
}
}
for (int i=0; i<vertex.size(); i++){
int tmp = vertex[i];
graph0[i+n].push_back(oldpi_first(vertex[i]));
if(oldpi_first(tmp) + oldpi_second(tmp) < 0 || oldpi_first(tmp) + oldpi_second(tmp) >= n)
continue;
tmp = oldpi(oldpi_first(tmp) + oldpi_second(tmp),oldpi_second(tmp));
int np = (int)(lower_bound(vertex.begin(),vertex.end(),tmp) - vertex.begin());
graph0[i+n].push_back(oldpi_first(vertex[i]));
if(np == vertex.size() || vertex[np] != tmp) continue;
graph1[i+n].push_back(np + n);
}
printf("%d\n",dijkstra(st,ed));
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
13544 KB |
Output is correct |
2 |
Correct |
0 ms |
13544 KB |
Output is correct |
3 |
Correct |
1 ms |
13544 KB |
Output is correct |
4 |
Correct |
0 ms |
13544 KB |
Output is correct |
5 |
Correct |
0 ms |
13544 KB |
Output is correct |
6 |
Correct |
0 ms |
13544 KB |
Output is correct |
7 |
Correct |
0 ms |
13544 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
13544 KB |
Output is correct |
2 |
Correct |
0 ms |
13544 KB |
Output is correct |
3 |
Correct |
0 ms |
13544 KB |
Output is correct |
4 |
Correct |
0 ms |
13544 KB |
Output is correct |
5 |
Correct |
0 ms |
13544 KB |
Output is correct |
6 |
Correct |
0 ms |
13544 KB |
Output is correct |
7 |
Correct |
0 ms |
13544 KB |
Output is correct |
8 |
Correct |
0 ms |
13544 KB |
Output is correct |
9 |
Correct |
0 ms |
13544 KB |
Output is correct |
10 |
Correct |
0 ms |
13684 KB |
Output is correct |
11 |
Correct |
0 ms |
14260 KB |
Output is correct |
12 |
Correct |
0 ms |
13544 KB |
Output is correct |
13 |
Correct |
0 ms |
13544 KB |
Output is correct |
14 |
Correct |
5 ms |
14324 KB |
Output is correct |
15 |
Correct |
4 ms |
14332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
13544 KB |
Output is correct |
2 |
Correct |
0 ms |
13544 KB |
Output is correct |
3 |
Correct |
0 ms |
13544 KB |
Output is correct |
4 |
Correct |
0 ms |
13544 KB |
Output is correct |
5 |
Correct |
0 ms |
13544 KB |
Output is correct |
6 |
Correct |
0 ms |
13544 KB |
Output is correct |
7 |
Correct |
0 ms |
13544 KB |
Output is correct |
8 |
Correct |
0 ms |
13544 KB |
Output is correct |
9 |
Correct |
0 ms |
13544 KB |
Output is correct |
10 |
Correct |
0 ms |
13684 KB |
Output is correct |
11 |
Correct |
5 ms |
14260 KB |
Output is correct |
12 |
Correct |
0 ms |
13544 KB |
Output is correct |
13 |
Correct |
0 ms |
13544 KB |
Output is correct |
14 |
Correct |
3 ms |
14324 KB |
Output is correct |
15 |
Correct |
5 ms |
14332 KB |
Output is correct |
16 |
Correct |
0 ms |
14052 KB |
Output is correct |
17 |
Correct |
10 ms |
15664 KB |
Output is correct |
18 |
Correct |
8 ms |
14528 KB |
Output is correct |
19 |
Correct |
5 ms |
14300 KB |
Output is correct |
20 |
Correct |
9 ms |
14228 KB |
Output is correct |
21 |
Correct |
0 ms |
13852 KB |
Output is correct |
22 |
Correct |
4 ms |
14352 KB |
Output is correct |
23 |
Correct |
8 ms |
14500 KB |
Output is correct |
24 |
Correct |
14 ms |
15284 KB |
Output is correct |
25 |
Correct |
11 ms |
14992 KB |
Output is correct |
26 |
Correct |
8 ms |
14748 KB |
Output is correct |
27 |
Correct |
7 ms |
14388 KB |
Output is correct |
28 |
Correct |
7 ms |
16532 KB |
Output is correct |
29 |
Correct |
33 ms |
21132 KB |
Output is correct |
30 |
Correct |
15 ms |
15908 KB |
Output is correct |
31 |
Correct |
15 ms |
17980 KB |
Output is correct |
32 |
Correct |
18 ms |
16632 KB |
Output is correct |
33 |
Correct |
64 ms |
28144 KB |
Output is correct |
34 |
Correct |
62 ms |
28168 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
13544 KB |
Output is correct |
2 |
Correct |
0 ms |
13544 KB |
Output is correct |
3 |
Correct |
0 ms |
13544 KB |
Output is correct |
4 |
Correct |
0 ms |
13544 KB |
Output is correct |
5 |
Correct |
0 ms |
13544 KB |
Output is correct |
6 |
Correct |
0 ms |
13544 KB |
Output is correct |
7 |
Correct |
0 ms |
13544 KB |
Output is correct |
8 |
Correct |
0 ms |
13544 KB |
Output is correct |
9 |
Correct |
0 ms |
13544 KB |
Output is correct |
10 |
Correct |
2 ms |
13684 KB |
Output is correct |
11 |
Correct |
4 ms |
14260 KB |
Output is correct |
12 |
Correct |
0 ms |
13544 KB |
Output is correct |
13 |
Correct |
0 ms |
13544 KB |
Output is correct |
14 |
Correct |
2 ms |
14324 KB |
Output is correct |
15 |
Correct |
5 ms |
14332 KB |
Output is correct |
16 |
Correct |
4 ms |
14052 KB |
Output is correct |
17 |
Correct |
10 ms |
15664 KB |
Output is correct |
18 |
Correct |
8 ms |
14528 KB |
Output is correct |
19 |
Correct |
5 ms |
14300 KB |
Output is correct |
20 |
Correct |
9 ms |
14228 KB |
Output is correct |
21 |
Correct |
0 ms |
13852 KB |
Output is correct |
22 |
Correct |
0 ms |
14352 KB |
Output is correct |
23 |
Correct |
6 ms |
14500 KB |
Output is correct |
24 |
Correct |
6 ms |
15284 KB |
Output is correct |
25 |
Correct |
8 ms |
14992 KB |
Output is correct |
26 |
Correct |
11 ms |
14748 KB |
Output is correct |
27 |
Correct |
9 ms |
14388 KB |
Output is correct |
28 |
Correct |
11 ms |
16532 KB |
Output is correct |
29 |
Correct |
35 ms |
21132 KB |
Output is correct |
30 |
Correct |
8 ms |
15908 KB |
Output is correct |
31 |
Correct |
18 ms |
17980 KB |
Output is correct |
32 |
Correct |
17 ms |
16632 KB |
Output is correct |
33 |
Correct |
69 ms |
28144 KB |
Output is correct |
34 |
Correct |
62 ms |
28168 KB |
Output is correct |
35 |
Correct |
79 ms |
29220 KB |
Output is correct |
36 |
Correct |
17 ms |
16136 KB |
Output is correct |
37 |
Correct |
103 ms |
37724 KB |
Output is correct |
38 |
Correct |
101 ms |
36900 KB |
Output is correct |
39 |
Correct |
115 ms |
37032 KB |
Output is correct |
40 |
Correct |
113 ms |
36844 KB |
Output is correct |
41 |
Correct |
109 ms |
36800 KB |
Output is correct |
42 |
Correct |
14 ms |
15120 KB |
Output is correct |
43 |
Correct |
13 ms |
14800 KB |
Output is correct |
44 |
Correct |
19 ms |
14692 KB |
Output is correct |
45 |
Correct |
283 ms |
72944 KB |
Output is correct |
46 |
Correct |
266 ms |
72956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
13544 KB |
Output is correct |
2 |
Correct |
0 ms |
13544 KB |
Output is correct |
3 |
Correct |
0 ms |
13544 KB |
Output is correct |
4 |
Correct |
0 ms |
13544 KB |
Output is correct |
5 |
Correct |
0 ms |
13544 KB |
Output is correct |
6 |
Correct |
0 ms |
13544 KB |
Output is correct |
7 |
Correct |
0 ms |
13544 KB |
Output is correct |
8 |
Correct |
0 ms |
13544 KB |
Output is correct |
9 |
Correct |
0 ms |
13544 KB |
Output is correct |
10 |
Correct |
0 ms |
13684 KB |
Output is correct |
11 |
Correct |
2 ms |
14260 KB |
Output is correct |
12 |
Correct |
0 ms |
13544 KB |
Output is correct |
13 |
Correct |
2 ms |
13544 KB |
Output is correct |
14 |
Correct |
4 ms |
14324 KB |
Output is correct |
15 |
Correct |
5 ms |
14332 KB |
Output is correct |
16 |
Correct |
4 ms |
14052 KB |
Output is correct |
17 |
Correct |
6 ms |
15664 KB |
Output is correct |
18 |
Correct |
9 ms |
14528 KB |
Output is correct |
19 |
Correct |
5 ms |
14300 KB |
Output is correct |
20 |
Correct |
6 ms |
14228 KB |
Output is correct |
21 |
Correct |
4 ms |
13852 KB |
Output is correct |
22 |
Correct |
5 ms |
14352 KB |
Output is correct |
23 |
Correct |
4 ms |
14500 KB |
Output is correct |
24 |
Correct |
6 ms |
15284 KB |
Output is correct |
25 |
Correct |
10 ms |
14992 KB |
Output is correct |
26 |
Correct |
11 ms |
14748 KB |
Output is correct |
27 |
Correct |
8 ms |
14388 KB |
Output is correct |
28 |
Correct |
13 ms |
16532 KB |
Output is correct |
29 |
Correct |
29 ms |
21132 KB |
Output is correct |
30 |
Correct |
16 ms |
15908 KB |
Output is correct |
31 |
Correct |
22 ms |
17980 KB |
Output is correct |
32 |
Correct |
14 ms |
16632 KB |
Output is correct |
33 |
Correct |
65 ms |
28144 KB |
Output is correct |
34 |
Correct |
64 ms |
28168 KB |
Output is correct |
35 |
Correct |
67 ms |
29220 KB |
Output is correct |
36 |
Correct |
17 ms |
16136 KB |
Output is correct |
37 |
Correct |
109 ms |
37724 KB |
Output is correct |
38 |
Correct |
102 ms |
36900 KB |
Output is correct |
39 |
Correct |
102 ms |
37032 KB |
Output is correct |
40 |
Correct |
111 ms |
36844 KB |
Output is correct |
41 |
Correct |
105 ms |
36800 KB |
Output is correct |
42 |
Correct |
13 ms |
15120 KB |
Output is correct |
43 |
Correct |
14 ms |
14800 KB |
Output is correct |
44 |
Correct |
15 ms |
14692 KB |
Output is correct |
45 |
Correct |
271 ms |
72944 KB |
Output is correct |
46 |
Correct |
259 ms |
72956 KB |
Output is correct |
47 |
Correct |
417 ms |
111656 KB |
Output is correct |
48 |
Correct |
194 ms |
45360 KB |
Output is correct |
49 |
Correct |
152 ms |
34704 KB |
Output is correct |
50 |
Correct |
151 ms |
34316 KB |
Output is correct |
51 |
Correct |
293 ms |
58052 KB |
Output is correct |
52 |
Correct |
310 ms |
63668 KB |
Output is correct |
53 |
Correct |
171 ms |
32988 KB |
Output is correct |
54 |
Correct |
87 ms |
19276 KB |
Output is correct |
55 |
Correct |
114 ms |
25672 KB |
Output is correct |
56 |
Correct |
126 ms |
24524 KB |
Output is correct |
57 |
Correct |
428 ms |
103960 KB |
Output is correct |
58 |
Correct |
131 ms |
27648 KB |
Output is correct |
59 |
Correct |
148 ms |
30060 KB |
Output is correct |
60 |
Correct |
188 ms |
37940 KB |
Output is correct |
61 |
Correct |
147 ms |
28960 KB |
Output is correct |
62 |
Correct |
370 ms |
77640 KB |
Output is correct |
63 |
Memory limit exceeded |
904 ms |
262144 KB |
Memory limit exceeded |
64 |
Halted |
0 ms |
0 KB |
- |