#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;
priority_queue<pi,vector<pi>,greater<pi> > pq;
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.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 : graph0[t.second]){
if(!vis[i]){
pq.push(pi(t.first,i));
}
};
for (auto &i : graph1[t.second]){
if(!vis[i]){
pq.push(pi(1 + t.first,i));
}
}
}
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));
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
13540 KB |
Output is correct |
2 |
Correct |
0 ms |
13540 KB |
Output is correct |
3 |
Correct |
1 ms |
13540 KB |
Output is correct |
4 |
Correct |
0 ms |
13540 KB |
Output is correct |
5 |
Correct |
0 ms |
13540 KB |
Output is correct |
6 |
Correct |
0 ms |
13540 KB |
Output is correct |
7 |
Correct |
0 ms |
13540 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
13540 KB |
Output is correct |
2 |
Correct |
0 ms |
13540 KB |
Output is correct |
3 |
Correct |
0 ms |
13540 KB |
Output is correct |
4 |
Correct |
0 ms |
13540 KB |
Output is correct |
5 |
Correct |
0 ms |
13540 KB |
Output is correct |
6 |
Correct |
0 ms |
13540 KB |
Output is correct |
7 |
Correct |
0 ms |
13540 KB |
Output is correct |
8 |
Correct |
0 ms |
13540 KB |
Output is correct |
9 |
Correct |
0 ms |
13540 KB |
Output is correct |
10 |
Correct |
2 ms |
13680 KB |
Output is correct |
11 |
Correct |
4 ms |
14256 KB |
Output is correct |
12 |
Correct |
2 ms |
13540 KB |
Output is correct |
13 |
Correct |
2 ms |
13540 KB |
Output is correct |
14 |
Correct |
3 ms |
14320 KB |
Output is correct |
15 |
Correct |
5 ms |
14328 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
13540 KB |
Output is correct |
2 |
Correct |
0 ms |
13540 KB |
Output is correct |
3 |
Correct |
0 ms |
13540 KB |
Output is correct |
4 |
Correct |
0 ms |
13540 KB |
Output is correct |
5 |
Correct |
0 ms |
13540 KB |
Output is correct |
6 |
Correct |
0 ms |
13540 KB |
Output is correct |
7 |
Correct |
1 ms |
13540 KB |
Output is correct |
8 |
Correct |
0 ms |
13540 KB |
Output is correct |
9 |
Correct |
0 ms |
13540 KB |
Output is correct |
10 |
Correct |
2 ms |
13680 KB |
Output is correct |
11 |
Correct |
2 ms |
14256 KB |
Output is correct |
12 |
Correct |
0 ms |
13540 KB |
Output is correct |
13 |
Correct |
0 ms |
13540 KB |
Output is correct |
14 |
Correct |
3 ms |
14320 KB |
Output is correct |
15 |
Correct |
0 ms |
14328 KB |
Output is correct |
16 |
Correct |
3 ms |
14044 KB |
Output is correct |
17 |
Correct |
9 ms |
15660 KB |
Output is correct |
18 |
Correct |
8 ms |
14524 KB |
Output is correct |
19 |
Correct |
8 ms |
14292 KB |
Output is correct |
20 |
Correct |
4 ms |
14224 KB |
Output is correct |
21 |
Correct |
4 ms |
13844 KB |
Output is correct |
22 |
Correct |
5 ms |
14348 KB |
Output is correct |
23 |
Correct |
9 ms |
14492 KB |
Output is correct |
24 |
Correct |
11 ms |
15280 KB |
Output is correct |
25 |
Correct |
10 ms |
14988 KB |
Output is correct |
26 |
Correct |
7 ms |
14744 KB |
Output is correct |
27 |
Correct |
6 ms |
14384 KB |
Output is correct |
28 |
Correct |
14 ms |
16528 KB |
Output is correct |
29 |
Correct |
40 ms |
21128 KB |
Output is correct |
30 |
Correct |
16 ms |
15896 KB |
Output is correct |
31 |
Correct |
27 ms |
17972 KB |
Output is correct |
32 |
Correct |
22 ms |
16628 KB |
Output is correct |
33 |
Correct |
65 ms |
28136 KB |
Output is correct |
34 |
Correct |
62 ms |
28164 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
13540 KB |
Output is correct |
2 |
Correct |
0 ms |
13540 KB |
Output is correct |
3 |
Correct |
0 ms |
13540 KB |
Output is correct |
4 |
Correct |
0 ms |
13540 KB |
Output is correct |
5 |
Correct |
0 ms |
13540 KB |
Output is correct |
6 |
Correct |
0 ms |
13540 KB |
Output is correct |
7 |
Correct |
0 ms |
13540 KB |
Output is correct |
8 |
Correct |
0 ms |
13540 KB |
Output is correct |
9 |
Correct |
1 ms |
13540 KB |
Output is correct |
10 |
Correct |
2 ms |
13680 KB |
Output is correct |
11 |
Correct |
0 ms |
14256 KB |
Output is correct |
12 |
Correct |
0 ms |
13540 KB |
Output is correct |
13 |
Correct |
0 ms |
13540 KB |
Output is correct |
14 |
Correct |
6 ms |
14320 KB |
Output is correct |
15 |
Correct |
5 ms |
14328 KB |
Output is correct |
16 |
Correct |
4 ms |
14044 KB |
Output is correct |
17 |
Correct |
6 ms |
15660 KB |
Output is correct |
18 |
Correct |
9 ms |
14524 KB |
Output is correct |
19 |
Correct |
8 ms |
14292 KB |
Output is correct |
20 |
Correct |
8 ms |
14224 KB |
Output is correct |
21 |
Correct |
0 ms |
13844 KB |
Output is correct |
22 |
Correct |
4 ms |
14348 KB |
Output is correct |
23 |
Correct |
6 ms |
14492 KB |
Output is correct |
24 |
Correct |
14 ms |
15280 KB |
Output is correct |
25 |
Correct |
11 ms |
14988 KB |
Output is correct |
26 |
Correct |
7 ms |
14744 KB |
Output is correct |
27 |
Correct |
5 ms |
14384 KB |
Output is correct |
28 |
Correct |
17 ms |
16528 KB |
Output is correct |
29 |
Correct |
34 ms |
21128 KB |
Output is correct |
30 |
Correct |
13 ms |
15896 KB |
Output is correct |
31 |
Correct |
27 ms |
17972 KB |
Output is correct |
32 |
Correct |
21 ms |
16628 KB |
Output is correct |
33 |
Correct |
73 ms |
28136 KB |
Output is correct |
34 |
Correct |
69 ms |
28164 KB |
Output is correct |
35 |
Correct |
78 ms |
29700 KB |
Output is correct |
36 |
Correct |
13 ms |
16132 KB |
Output is correct |
37 |
Correct |
109 ms |
37976 KB |
Output is correct |
38 |
Correct |
111 ms |
36900 KB |
Output is correct |
39 |
Correct |
112 ms |
37032 KB |
Output is correct |
40 |
Correct |
103 ms |
36840 KB |
Output is correct |
41 |
Correct |
110 ms |
36972 KB |
Output is correct |
42 |
Correct |
14 ms |
15116 KB |
Output is correct |
43 |
Correct |
14 ms |
14796 KB |
Output is correct |
44 |
Correct |
14 ms |
15188 KB |
Output is correct |
45 |
Correct |
331 ms |
73192 KB |
Output is correct |
46 |
Correct |
281 ms |
73228 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
13540 KB |
Output is correct |
2 |
Correct |
0 ms |
13540 KB |
Output is correct |
3 |
Correct |
0 ms |
13540 KB |
Output is correct |
4 |
Correct |
0 ms |
13540 KB |
Output is correct |
5 |
Correct |
0 ms |
13540 KB |
Output is correct |
6 |
Correct |
0 ms |
13540 KB |
Output is correct |
7 |
Correct |
0 ms |
13540 KB |
Output is correct |
8 |
Correct |
0 ms |
13540 KB |
Output is correct |
9 |
Correct |
1 ms |
13540 KB |
Output is correct |
10 |
Correct |
2 ms |
13680 KB |
Output is correct |
11 |
Correct |
4 ms |
14256 KB |
Output is correct |
12 |
Correct |
0 ms |
13540 KB |
Output is correct |
13 |
Correct |
0 ms |
13540 KB |
Output is correct |
14 |
Correct |
6 ms |
14320 KB |
Output is correct |
15 |
Correct |
3 ms |
14328 KB |
Output is correct |
16 |
Correct |
2 ms |
14044 KB |
Output is correct |
17 |
Correct |
11 ms |
15660 KB |
Output is correct |
18 |
Correct |
6 ms |
14524 KB |
Output is correct |
19 |
Correct |
5 ms |
14292 KB |
Output is correct |
20 |
Correct |
9 ms |
14224 KB |
Output is correct |
21 |
Correct |
0 ms |
13844 KB |
Output is correct |
22 |
Correct |
8 ms |
14348 KB |
Output is correct |
23 |
Correct |
6 ms |
14492 KB |
Output is correct |
24 |
Correct |
10 ms |
15280 KB |
Output is correct |
25 |
Correct |
13 ms |
14988 KB |
Output is correct |
26 |
Correct |
11 ms |
14744 KB |
Output is correct |
27 |
Correct |
8 ms |
14384 KB |
Output is correct |
28 |
Correct |
15 ms |
16528 KB |
Output is correct |
29 |
Correct |
37 ms |
21128 KB |
Output is correct |
30 |
Correct |
16 ms |
15896 KB |
Output is correct |
31 |
Correct |
22 ms |
17972 KB |
Output is correct |
32 |
Correct |
18 ms |
16628 KB |
Output is correct |
33 |
Correct |
75 ms |
28136 KB |
Output is correct |
34 |
Correct |
70 ms |
28164 KB |
Output is correct |
35 |
Correct |
81 ms |
29700 KB |
Output is correct |
36 |
Correct |
11 ms |
16132 KB |
Output is correct |
37 |
Correct |
100 ms |
37976 KB |
Output is correct |
38 |
Correct |
105 ms |
36900 KB |
Output is correct |
39 |
Correct |
116 ms |
37032 KB |
Output is correct |
40 |
Correct |
115 ms |
36840 KB |
Output is correct |
41 |
Correct |
115 ms |
36972 KB |
Output is correct |
42 |
Correct |
17 ms |
15116 KB |
Output is correct |
43 |
Correct |
14 ms |
14796 KB |
Output is correct |
44 |
Correct |
18 ms |
15188 KB |
Output is correct |
45 |
Correct |
320 ms |
73192 KB |
Output is correct |
46 |
Correct |
287 ms |
73228 KB |
Output is correct |
47 |
Correct |
419 ms |
112212 KB |
Output is correct |
48 |
Correct |
197 ms |
45352 KB |
Output is correct |
49 |
Correct |
156 ms |
34700 KB |
Output is correct |
50 |
Correct |
161 ms |
34312 KB |
Output is correct |
51 |
Correct |
322 ms |
58224 KB |
Output is correct |
52 |
Correct |
331 ms |
63876 KB |
Output is correct |
53 |
Correct |
162 ms |
32984 KB |
Output is correct |
54 |
Correct |
91 ms |
19272 KB |
Output is correct |
55 |
Correct |
116 ms |
25668 KB |
Output is correct |
56 |
Correct |
126 ms |
24520 KB |
Output is correct |
57 |
Correct |
428 ms |
103956 KB |
Output is correct |
58 |
Correct |
152 ms |
28504 KB |
Output is correct |
59 |
Correct |
160 ms |
30056 KB |
Output is correct |
60 |
Correct |
186 ms |
37804 KB |
Output is correct |
61 |
Correct |
152 ms |
28952 KB |
Output is correct |
62 |
Correct |
379 ms |
77636 KB |
Output is correct |
63 |
Memory limit exceeded |
902 ms |
262144 KB |
Memory limit exceeded |
64 |
Halted |
0 ms |
0 KB |
- |