#include <cstdio>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
int m, n;
vector<int> vertex;
vector<int> dx[30005];
bool r_dir[30005][180], l_dir[30005][180];
int px[30005], py[30005];
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 bfs(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(graph0[qf] != 0 && !vis[graph0[qf]]){
pq[dist%2].push(graph0[qf]);
}
if(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",&px[i],&py[i]);
x = px[i];
y = py[i];
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);
}
}
}
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 t=0; t<m; t++) {
int i = px[t];
int j = py[t];
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]);
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",bfs(st,ed));
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
13780 KB |
Output is correct |
2 |
Correct |
0 ms |
13780 KB |
Output is correct |
3 |
Correct |
0 ms |
13780 KB |
Output is correct |
4 |
Correct |
0 ms |
13780 KB |
Output is correct |
5 |
Correct |
0 ms |
13780 KB |
Output is correct |
6 |
Correct |
0 ms |
13780 KB |
Output is correct |
7 |
Correct |
0 ms |
13780 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
13780 KB |
Output is correct |
2 |
Correct |
0 ms |
13780 KB |
Output is correct |
3 |
Correct |
0 ms |
13780 KB |
Output is correct |
4 |
Correct |
0 ms |
13780 KB |
Output is correct |
5 |
Correct |
0 ms |
13780 KB |
Output is correct |
6 |
Correct |
0 ms |
13780 KB |
Output is correct |
7 |
Correct |
0 ms |
13780 KB |
Output is correct |
8 |
Correct |
0 ms |
13780 KB |
Output is correct |
9 |
Correct |
0 ms |
13780 KB |
Output is correct |
10 |
Correct |
0 ms |
13780 KB |
Output is correct |
11 |
Correct |
0 ms |
13920 KB |
Output is correct |
12 |
Correct |
0 ms |
13780 KB |
Output is correct |
13 |
Correct |
0 ms |
13780 KB |
Output is correct |
14 |
Correct |
3 ms |
13928 KB |
Output is correct |
15 |
Correct |
4 ms |
13928 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
13780 KB |
Output is correct |
2 |
Correct |
0 ms |
13780 KB |
Output is correct |
3 |
Correct |
0 ms |
13780 KB |
Output is correct |
4 |
Correct |
0 ms |
13780 KB |
Output is correct |
5 |
Correct |
0 ms |
13780 KB |
Output is correct |
6 |
Correct |
0 ms |
13780 KB |
Output is correct |
7 |
Correct |
0 ms |
13780 KB |
Output is correct |
8 |
Correct |
0 ms |
13780 KB |
Output is correct |
9 |
Correct |
0 ms |
13780 KB |
Output is correct |
10 |
Correct |
2 ms |
13780 KB |
Output is correct |
11 |
Correct |
4 ms |
13920 KB |
Output is correct |
12 |
Correct |
0 ms |
13780 KB |
Output is correct |
13 |
Correct |
0 ms |
13780 KB |
Output is correct |
14 |
Correct |
0 ms |
13928 KB |
Output is correct |
15 |
Correct |
0 ms |
13928 KB |
Output is correct |
16 |
Correct |
0 ms |
13916 KB |
Output is correct |
17 |
Correct |
4 ms |
14188 KB |
Output is correct |
18 |
Correct |
8 ms |
13928 KB |
Output is correct |
19 |
Correct |
4 ms |
13912 KB |
Output is correct |
20 |
Correct |
4 ms |
13924 KB |
Output is correct |
21 |
Correct |
3 ms |
13780 KB |
Output is correct |
22 |
Correct |
5 ms |
13920 KB |
Output is correct |
23 |
Correct |
4 ms |
13924 KB |
Output is correct |
24 |
Correct |
10 ms |
14100 KB |
Output is correct |
25 |
Correct |
6 ms |
14076 KB |
Output is correct |
26 |
Correct |
9 ms |
14068 KB |
Output is correct |
27 |
Correct |
7 ms |
13928 KB |
Output is correct |
28 |
Incorrect |
14 ms |
14356 KB |
Output isn't correct |
29 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
13780 KB |
Output is correct |
2 |
Correct |
0 ms |
13780 KB |
Output is correct |
3 |
Correct |
0 ms |
13780 KB |
Output is correct |
4 |
Correct |
0 ms |
13780 KB |
Output is correct |
5 |
Correct |
0 ms |
13780 KB |
Output is correct |
6 |
Correct |
0 ms |
13780 KB |
Output is correct |
7 |
Correct |
0 ms |
13780 KB |
Output is correct |
8 |
Correct |
0 ms |
13780 KB |
Output is correct |
9 |
Correct |
0 ms |
13780 KB |
Output is correct |
10 |
Correct |
0 ms |
13780 KB |
Output is correct |
11 |
Correct |
3 ms |
13920 KB |
Output is correct |
12 |
Correct |
0 ms |
13780 KB |
Output is correct |
13 |
Correct |
2 ms |
13780 KB |
Output is correct |
14 |
Correct |
4 ms |
13928 KB |
Output is correct |
15 |
Correct |
2 ms |
13928 KB |
Output is correct |
16 |
Correct |
0 ms |
13916 KB |
Output is correct |
17 |
Correct |
8 ms |
14188 KB |
Output is correct |
18 |
Correct |
8 ms |
13928 KB |
Output is correct |
19 |
Correct |
3 ms |
13912 KB |
Output is correct |
20 |
Correct |
6 ms |
13924 KB |
Output is correct |
21 |
Correct |
3 ms |
13780 KB |
Output is correct |
22 |
Correct |
3 ms |
13920 KB |
Output is correct |
23 |
Correct |
3 ms |
13924 KB |
Output is correct |
24 |
Correct |
8 ms |
14100 KB |
Output is correct |
25 |
Correct |
7 ms |
14076 KB |
Output is correct |
26 |
Correct |
6 ms |
14068 KB |
Output is correct |
27 |
Correct |
7 ms |
13928 KB |
Output is correct |
28 |
Incorrect |
13 ms |
14356 KB |
Output isn't correct |
29 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
13780 KB |
Output is correct |
2 |
Correct |
1 ms |
13780 KB |
Output is correct |
3 |
Correct |
0 ms |
13780 KB |
Output is correct |
4 |
Correct |
0 ms |
13780 KB |
Output is correct |
5 |
Correct |
0 ms |
13780 KB |
Output is correct |
6 |
Correct |
0 ms |
13780 KB |
Output is correct |
7 |
Correct |
0 ms |
13780 KB |
Output is correct |
8 |
Correct |
0 ms |
13780 KB |
Output is correct |
9 |
Correct |
0 ms |
13780 KB |
Output is correct |
10 |
Correct |
0 ms |
13780 KB |
Output is correct |
11 |
Correct |
3 ms |
13920 KB |
Output is correct |
12 |
Correct |
0 ms |
13780 KB |
Output is correct |
13 |
Correct |
0 ms |
13780 KB |
Output is correct |
14 |
Correct |
0 ms |
13928 KB |
Output is correct |
15 |
Correct |
4 ms |
13928 KB |
Output is correct |
16 |
Correct |
0 ms |
13916 KB |
Output is correct |
17 |
Correct |
8 ms |
14188 KB |
Output is correct |
18 |
Correct |
6 ms |
13928 KB |
Output is correct |
19 |
Correct |
5 ms |
13912 KB |
Output is correct |
20 |
Correct |
8 ms |
13924 KB |
Output is correct |
21 |
Correct |
3 ms |
13780 KB |
Output is correct |
22 |
Correct |
5 ms |
13920 KB |
Output is correct |
23 |
Correct |
4 ms |
13924 KB |
Output is correct |
24 |
Correct |
7 ms |
14100 KB |
Output is correct |
25 |
Correct |
10 ms |
14076 KB |
Output is correct |
26 |
Correct |
9 ms |
14068 KB |
Output is correct |
27 |
Correct |
7 ms |
13928 KB |
Output is correct |
28 |
Incorrect |
13 ms |
14356 KB |
Output isn't correct |
29 |
Halted |
0 ms |
0 KB |
- |