#include <cstdio>
#include <map>
#include <vector>
#include <algorithm>
#include <utility>
#include <queue>
using namespace std;
int m, n;
vector<int> vertex;
vector<int> dummy[30005];
vector<int> dx[30005];
bool r_dir[30005][180], l_dir[30005][180];
vector<int> graph[30005];
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;
if(qf < n){
for (auto &i : graph[qf]){
if(!vis[i]){
pq[dist%2].push(i);
}
}
}
if(qf >= n && graph0[qf] != 0 && !vis[graph0[qf] - 1]){
pq[dist%2].push(graph0[qf] - 1);
}
if(qf >= n && graph1[qf] != 0 && !vis[graph1[qf]]){
pq[(dist+1)%2].push(graph1[qf]);
}
}
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 < 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)){
graph[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)){
graph[i].push_back(pos + n);
}
}
}
for (int i=0; i<vertex.size(); i++){
int tmp = vertex[i];
graph0[i+n] = oldpi_first(vertex[i]) + 1;
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());
if(np == vertex.size() || vertex[np] != tmp) continue;
graph1[i+n] = np + n;
}
printf("%d\n",dijkstra(st,ed));
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
14248 KB |
Output is correct |
2 |
Correct |
0 ms |
14248 KB |
Output is correct |
3 |
Correct |
0 ms |
14248 KB |
Output is correct |
4 |
Correct |
0 ms |
14248 KB |
Output is correct |
5 |
Correct |
0 ms |
14248 KB |
Output is correct |
6 |
Correct |
0 ms |
14248 KB |
Output is correct |
7 |
Correct |
0 ms |
14248 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
14248 KB |
Output is correct |
2 |
Correct |
0 ms |
14248 KB |
Output is correct |
3 |
Correct |
0 ms |
14248 KB |
Output is correct |
4 |
Correct |
0 ms |
14248 KB |
Output is correct |
5 |
Correct |
0 ms |
14248 KB |
Output is correct |
6 |
Correct |
0 ms |
14248 KB |
Output is correct |
7 |
Correct |
0 ms |
14248 KB |
Output is correct |
8 |
Correct |
0 ms |
14248 KB |
Output is correct |
9 |
Correct |
0 ms |
14248 KB |
Output is correct |
10 |
Correct |
3 ms |
14248 KB |
Output is correct |
11 |
Correct |
4 ms |
14380 KB |
Output is correct |
12 |
Correct |
0 ms |
14248 KB |
Output is correct |
13 |
Correct |
2 ms |
14248 KB |
Output is correct |
14 |
Correct |
4 ms |
14408 KB |
Output is correct |
15 |
Correct |
2 ms |
14408 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
14248 KB |
Output is correct |
2 |
Correct |
0 ms |
14248 KB |
Output is correct |
3 |
Correct |
0 ms |
14248 KB |
Output is correct |
4 |
Correct |
0 ms |
14248 KB |
Output is correct |
5 |
Correct |
0 ms |
14248 KB |
Output is correct |
6 |
Correct |
0 ms |
14248 KB |
Output is correct |
7 |
Correct |
1 ms |
14248 KB |
Output is correct |
8 |
Correct |
0 ms |
14248 KB |
Output is correct |
9 |
Correct |
1 ms |
14248 KB |
Output is correct |
10 |
Correct |
0 ms |
14248 KB |
Output is correct |
11 |
Correct |
0 ms |
14380 KB |
Output is correct |
12 |
Correct |
2 ms |
14248 KB |
Output is correct |
13 |
Correct |
2 ms |
14248 KB |
Output is correct |
14 |
Correct |
0 ms |
14408 KB |
Output is correct |
15 |
Correct |
3 ms |
14408 KB |
Output is correct |
16 |
Correct |
0 ms |
14388 KB |
Output is correct |
17 |
Correct |
9 ms |
14764 KB |
Output is correct |
18 |
Correct |
4 ms |
14388 KB |
Output is correct |
19 |
Correct |
6 ms |
14396 KB |
Output is correct |
20 |
Correct |
7 ms |
14380 KB |
Output is correct |
21 |
Correct |
3 ms |
14248 KB |
Output is correct |
22 |
Correct |
7 ms |
14408 KB |
Output is correct |
23 |
Correct |
4 ms |
14384 KB |
Output is correct |
24 |
Correct |
11 ms |
14552 KB |
Output is correct |
25 |
Correct |
7 ms |
14528 KB |
Output is correct |
26 |
Correct |
9 ms |
14548 KB |
Output is correct |
27 |
Correct |
7 ms |
14404 KB |
Output is correct |
28 |
Correct |
14 ms |
14892 KB |
Output is correct |
29 |
Correct |
21 ms |
15448 KB |
Output is correct |
30 |
Correct |
7 ms |
14780 KB |
Output is correct |
31 |
Correct |
15 ms |
15116 KB |
Output is correct |
32 |
Correct |
12 ms |
14812 KB |
Output is correct |
33 |
Correct |
33 ms |
16412 KB |
Output is correct |
34 |
Correct |
33 ms |
16428 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
14248 KB |
Output is correct |
2 |
Correct |
0 ms |
14248 KB |
Output is correct |
3 |
Correct |
0 ms |
14248 KB |
Output is correct |
4 |
Correct |
0 ms |
14248 KB |
Output is correct |
5 |
Correct |
0 ms |
14248 KB |
Output is correct |
6 |
Correct |
0 ms |
14248 KB |
Output is correct |
7 |
Correct |
0 ms |
14248 KB |
Output is correct |
8 |
Correct |
0 ms |
14248 KB |
Output is correct |
9 |
Correct |
0 ms |
14248 KB |
Output is correct |
10 |
Correct |
0 ms |
14248 KB |
Output is correct |
11 |
Correct |
0 ms |
14380 KB |
Output is correct |
12 |
Correct |
0 ms |
14248 KB |
Output is correct |
13 |
Correct |
0 ms |
14248 KB |
Output is correct |
14 |
Correct |
4 ms |
14408 KB |
Output is correct |
15 |
Correct |
2 ms |
14408 KB |
Output is correct |
16 |
Correct |
3 ms |
14388 KB |
Output is correct |
17 |
Correct |
8 ms |
14764 KB |
Output is correct |
18 |
Correct |
6 ms |
14388 KB |
Output is correct |
19 |
Correct |
4 ms |
14396 KB |
Output is correct |
20 |
Correct |
7 ms |
14380 KB |
Output is correct |
21 |
Correct |
3 ms |
14248 KB |
Output is correct |
22 |
Correct |
6 ms |
14408 KB |
Output is correct |
23 |
Correct |
6 ms |
14384 KB |
Output is correct |
24 |
Correct |
8 ms |
14552 KB |
Output is correct |
25 |
Correct |
10 ms |
14528 KB |
Output is correct |
26 |
Correct |
10 ms |
14548 KB |
Output is correct |
27 |
Correct |
7 ms |
14404 KB |
Output is correct |
28 |
Correct |
7 ms |
14892 KB |
Output is correct |
29 |
Correct |
22 ms |
15448 KB |
Output is correct |
30 |
Correct |
7 ms |
14780 KB |
Output is correct |
31 |
Correct |
11 ms |
15116 KB |
Output is correct |
32 |
Correct |
12 ms |
14812 KB |
Output is correct |
33 |
Correct |
34 ms |
16412 KB |
Output is correct |
34 |
Correct |
30 ms |
16428 KB |
Output is correct |
35 |
Correct |
54 ms |
17692 KB |
Output is correct |
36 |
Correct |
8 ms |
14832 KB |
Output is correct |
37 |
Correct |
65 ms |
18480 KB |
Output is correct |
38 |
Correct |
71 ms |
18740 KB |
Output is correct |
39 |
Correct |
69 ms |
18504 KB |
Output is correct |
40 |
Correct |
69 ms |
18488 KB |
Output is correct |
41 |
Correct |
64 ms |
18608 KB |
Output is correct |
42 |
Correct |
17 ms |
15056 KB |
Output is correct |
43 |
Correct |
17 ms |
14960 KB |
Output is correct |
44 |
Correct |
13 ms |
15016 KB |
Output is correct |
45 |
Correct |
151 ms |
23456 KB |
Output is correct |
46 |
Correct |
152 ms |
23448 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
14248 KB |
Output is correct |
2 |
Correct |
0 ms |
14248 KB |
Output is correct |
3 |
Correct |
0 ms |
14248 KB |
Output is correct |
4 |
Correct |
0 ms |
14248 KB |
Output is correct |
5 |
Correct |
1 ms |
14248 KB |
Output is correct |
6 |
Correct |
0 ms |
14248 KB |
Output is correct |
7 |
Correct |
0 ms |
14248 KB |
Output is correct |
8 |
Correct |
0 ms |
14248 KB |
Output is correct |
9 |
Correct |
0 ms |
14248 KB |
Output is correct |
10 |
Correct |
0 ms |
14248 KB |
Output is correct |
11 |
Correct |
0 ms |
14380 KB |
Output is correct |
12 |
Correct |
0 ms |
14248 KB |
Output is correct |
13 |
Correct |
0 ms |
14248 KB |
Output is correct |
14 |
Correct |
3 ms |
14408 KB |
Output is correct |
15 |
Correct |
4 ms |
14408 KB |
Output is correct |
16 |
Correct |
4 ms |
14388 KB |
Output is correct |
17 |
Correct |
8 ms |
14764 KB |
Output is correct |
18 |
Correct |
6 ms |
14388 KB |
Output is correct |
19 |
Correct |
4 ms |
14396 KB |
Output is correct |
20 |
Correct |
4 ms |
14380 KB |
Output is correct |
21 |
Correct |
3 ms |
14248 KB |
Output is correct |
22 |
Correct |
5 ms |
14408 KB |
Output is correct |
23 |
Correct |
6 ms |
14384 KB |
Output is correct |
24 |
Correct |
8 ms |
14552 KB |
Output is correct |
25 |
Correct |
8 ms |
14528 KB |
Output is correct |
26 |
Correct |
5 ms |
14548 KB |
Output is correct |
27 |
Correct |
4 ms |
14404 KB |
Output is correct |
28 |
Correct |
14 ms |
14892 KB |
Output is correct |
29 |
Correct |
22 ms |
15448 KB |
Output is correct |
30 |
Correct |
7 ms |
14780 KB |
Output is correct |
31 |
Correct |
15 ms |
15116 KB |
Output is correct |
32 |
Correct |
9 ms |
14812 KB |
Output is correct |
33 |
Correct |
36 ms |
16412 KB |
Output is correct |
34 |
Correct |
33 ms |
16428 KB |
Output is correct |
35 |
Correct |
51 ms |
17692 KB |
Output is correct |
36 |
Correct |
12 ms |
14832 KB |
Output is correct |
37 |
Correct |
64 ms |
18480 KB |
Output is correct |
38 |
Correct |
72 ms |
18740 KB |
Output is correct |
39 |
Correct |
72 ms |
18504 KB |
Output is correct |
40 |
Correct |
75 ms |
18488 KB |
Output is correct |
41 |
Correct |
72 ms |
18608 KB |
Output is correct |
42 |
Correct |
14 ms |
15056 KB |
Output is correct |
43 |
Correct |
12 ms |
14960 KB |
Output is correct |
44 |
Correct |
10 ms |
15016 KB |
Output is correct |
45 |
Correct |
156 ms |
23456 KB |
Output is correct |
46 |
Correct |
145 ms |
23448 KB |
Output is correct |
47 |
Correct |
272 ms |
31388 KB |
Output is correct |
48 |
Correct |
153 ms |
20240 KB |
Output is correct |
49 |
Correct |
128 ms |
18968 KB |
Output is correct |
50 |
Correct |
132 ms |
18732 KB |
Output is correct |
51 |
Correct |
215 ms |
23104 KB |
Output is correct |
52 |
Correct |
234 ms |
23680 KB |
Output is correct |
53 |
Correct |
142 ms |
19136 KB |
Output is correct |
54 |
Correct |
86 ms |
15788 KB |
Output is correct |
55 |
Correct |
100 ms |
16608 KB |
Output is correct |
56 |
Correct |
111 ms |
18012 KB |
Output is correct |
57 |
Correct |
263 ms |
28908 KB |
Output is correct |
58 |
Correct |
110 ms |
17584 KB |
Output is correct |
59 |
Correct |
121 ms |
17920 KB |
Output is correct |
60 |
Correct |
147 ms |
19376 KB |
Output is correct |
61 |
Correct |
126 ms |
17800 KB |
Output is correct |
62 |
Correct |
261 ms |
26028 KB |
Output is correct |
63 |
Correct |
849 ms |
66944 KB |
Output is correct |
64 |
Execution timed out |
1000 ms |
71176 KB |
Program timed out |
65 |
Halted |
0 ms |
0 KB |
- |