#include <cstdio>
#include <cstdlib>
#include <vector>
#include <algorithm>
#include <utility>
#include <queue>
using namespace std;
int m, n;
int piv;
vector<int> dx[30005], label[30005];
bool r_dir[30005][180], l_dir[30005][180];
int px[30005], py[30005];
vector<int> graph[30005];
int *graph0, *graph1;
bool *vis;
queue<int> pq[2];
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);
px[i] = x;
py[i] = 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);
}
}
}
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);
}
}
}
piv = n;
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]){
label[i].push_back(piv++);
}
}
graph0 = (int*) calloc(piv,sizeof(int));
graph1 = (int*) calloc(piv,sizeof(int));
vis = (bool*) calloc(piv,sizeof(bool));
for(int t=0; t<m; t++){
int i = px[t];
int j = py[t];
int pos = (int)(lower_bound(dx[i].begin(),dx[i].end(),j) - dx[i].begin());
if(pos != dx[i].size() && dx[i][pos] == j){
graph[i].push_back(label[i][pos]);
}
pos = (int)(lower_bound(dx[i].begin(),dx[i].end(),-j) - dx[i].begin());
if(pos != dx[i].size() && dx[i][pos] == -j){
graph[i].push_back(label[i][pos]);
}
}
int piv = n;
for (int i=0; i<n; i++){
for (int j=0; j<dx[i].size(); j++) {
int tmp_first = i;
int tmp_second = dx[i][j];
graph0[piv++] = i+1;
if(tmp_first + tmp_second < 0 || tmp_first + tmp_second >= n) continue;
tmp_first += tmp_second;
int pos = (int)(lower_bound(dx[tmp_first].begin(),dx[tmp_first].end(),tmp_second)
- dx[tmp_first].begin());
if(pos == dx[tmp_first].size() || dx[tmp_first][pos] != tmp_second) continue;
graph1[piv - 1] = label[tmp_first][pos];
}
}
printf("%d\n",dijkstra(st,ed));
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
14476 KB |
Output is correct |
2 |
Correct |
0 ms |
14476 KB |
Output is correct |
3 |
Correct |
0 ms |
14476 KB |
Output is correct |
4 |
Correct |
0 ms |
14476 KB |
Output is correct |
5 |
Correct |
0 ms |
14476 KB |
Output is correct |
6 |
Correct |
0 ms |
14476 KB |
Output is correct |
7 |
Correct |
0 ms |
14476 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
14476 KB |
Output is correct |
2 |
Correct |
0 ms |
14476 KB |
Output is correct |
3 |
Correct |
0 ms |
14476 KB |
Output is correct |
4 |
Correct |
0 ms |
14476 KB |
Output is correct |
5 |
Correct |
2 ms |
14476 KB |
Output is correct |
6 |
Correct |
0 ms |
14476 KB |
Output is correct |
7 |
Correct |
0 ms |
14476 KB |
Output is correct |
8 |
Correct |
0 ms |
14476 KB |
Output is correct |
9 |
Correct |
0 ms |
14476 KB |
Output is correct |
10 |
Correct |
0 ms |
14476 KB |
Output is correct |
11 |
Correct |
0 ms |
14628 KB |
Output is correct |
12 |
Correct |
0 ms |
14476 KB |
Output is correct |
13 |
Correct |
0 ms |
14476 KB |
Output is correct |
14 |
Correct |
3 ms |
14608 KB |
Output is correct |
15 |
Correct |
0 ms |
14608 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
14476 KB |
Output is correct |
2 |
Correct |
0 ms |
14476 KB |
Output is correct |
3 |
Correct |
0 ms |
14476 KB |
Output is correct |
4 |
Correct |
0 ms |
14476 KB |
Output is correct |
5 |
Correct |
0 ms |
14476 KB |
Output is correct |
6 |
Correct |
0 ms |
14476 KB |
Output is correct |
7 |
Correct |
0 ms |
14476 KB |
Output is correct |
8 |
Correct |
0 ms |
14476 KB |
Output is correct |
9 |
Correct |
0 ms |
14476 KB |
Output is correct |
10 |
Correct |
0 ms |
14476 KB |
Output is correct |
11 |
Correct |
4 ms |
14628 KB |
Output is correct |
12 |
Correct |
0 ms |
14476 KB |
Output is correct |
13 |
Correct |
0 ms |
14476 KB |
Output is correct |
14 |
Correct |
0 ms |
14608 KB |
Output is correct |
15 |
Correct |
0 ms |
14608 KB |
Output is correct |
16 |
Correct |
0 ms |
14476 KB |
Output is correct |
17 |
Correct |
4 ms |
14776 KB |
Output is correct |
18 |
Correct |
8 ms |
14608 KB |
Output is correct |
19 |
Correct |
7 ms |
14632 KB |
Output is correct |
20 |
Correct |
7 ms |
14628 KB |
Output is correct |
21 |
Correct |
0 ms |
14476 KB |
Output is correct |
22 |
Correct |
6 ms |
14608 KB |
Output is correct |
23 |
Correct |
3 ms |
14608 KB |
Output is correct |
24 |
Correct |
6 ms |
14760 KB |
Output is correct |
25 |
Correct |
6 ms |
14768 KB |
Output is correct |
26 |
Correct |
4 ms |
14744 KB |
Output is correct |
27 |
Correct |
5 ms |
14608 KB |
Output is correct |
28 |
Correct |
7 ms |
15048 KB |
Output is correct |
29 |
Correct |
9 ms |
15840 KB |
Output is correct |
30 |
Correct |
7 ms |
14944 KB |
Output is correct |
31 |
Correct |
10 ms |
15300 KB |
Output is correct |
32 |
Correct |
6 ms |
15044 KB |
Output is correct |
33 |
Correct |
20 ms |
16808 KB |
Output is correct |
34 |
Correct |
20 ms |
16836 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
14476 KB |
Output is correct |
2 |
Correct |
0 ms |
14476 KB |
Output is correct |
3 |
Correct |
0 ms |
14476 KB |
Output is correct |
4 |
Correct |
2 ms |
14476 KB |
Output is correct |
5 |
Correct |
2 ms |
14476 KB |
Output is correct |
6 |
Correct |
0 ms |
14476 KB |
Output is correct |
7 |
Correct |
0 ms |
14476 KB |
Output is correct |
8 |
Correct |
0 ms |
14476 KB |
Output is correct |
9 |
Correct |
0 ms |
14476 KB |
Output is correct |
10 |
Correct |
0 ms |
14476 KB |
Output is correct |
11 |
Correct |
3 ms |
14628 KB |
Output is correct |
12 |
Correct |
0 ms |
14476 KB |
Output is correct |
13 |
Correct |
0 ms |
14476 KB |
Output is correct |
14 |
Correct |
0 ms |
14608 KB |
Output is correct |
15 |
Correct |
1 ms |
14608 KB |
Output is correct |
16 |
Correct |
0 ms |
14476 KB |
Output is correct |
17 |
Correct |
4 ms |
14776 KB |
Output is correct |
18 |
Correct |
3 ms |
14608 KB |
Output is correct |
19 |
Correct |
7 ms |
14632 KB |
Output is correct |
20 |
Correct |
4 ms |
14628 KB |
Output is correct |
21 |
Correct |
0 ms |
14476 KB |
Output is correct |
22 |
Correct |
4 ms |
14608 KB |
Output is correct |
23 |
Correct |
3 ms |
14608 KB |
Output is correct |
24 |
Correct |
7 ms |
14760 KB |
Output is correct |
25 |
Correct |
6 ms |
14768 KB |
Output is correct |
26 |
Correct |
7 ms |
14744 KB |
Output is correct |
27 |
Correct |
8 ms |
14608 KB |
Output is correct |
28 |
Correct |
11 ms |
15048 KB |
Output is correct |
29 |
Correct |
12 ms |
15840 KB |
Output is correct |
30 |
Correct |
9 ms |
14944 KB |
Output is correct |
31 |
Correct |
6 ms |
15300 KB |
Output is correct |
32 |
Correct |
4 ms |
15044 KB |
Output is correct |
33 |
Correct |
21 ms |
16808 KB |
Output is correct |
34 |
Correct |
15 ms |
16836 KB |
Output is correct |
35 |
Correct |
32 ms |
17672 KB |
Output is correct |
36 |
Correct |
4 ms |
15004 KB |
Output is correct |
37 |
Correct |
34 ms |
18708 KB |
Output is correct |
38 |
Correct |
40 ms |
18936 KB |
Output is correct |
39 |
Correct |
49 ms |
18820 KB |
Output is correct |
40 |
Correct |
46 ms |
18812 KB |
Output is correct |
41 |
Correct |
46 ms |
18804 KB |
Output is correct |
42 |
Correct |
13 ms |
15132 KB |
Output is correct |
43 |
Correct |
15 ms |
15088 KB |
Output is correct |
44 |
Correct |
9 ms |
15084 KB |
Output is correct |
45 |
Correct |
98 ms |
24148 KB |
Output is correct |
46 |
Correct |
98 ms |
24152 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
14476 KB |
Output is correct |
2 |
Correct |
0 ms |
14476 KB |
Output is correct |
3 |
Correct |
0 ms |
14476 KB |
Output is correct |
4 |
Correct |
0 ms |
14476 KB |
Output is correct |
5 |
Correct |
0 ms |
14476 KB |
Output is correct |
6 |
Correct |
0 ms |
14476 KB |
Output is correct |
7 |
Correct |
0 ms |
14476 KB |
Output is correct |
8 |
Correct |
0 ms |
14476 KB |
Output is correct |
9 |
Correct |
0 ms |
14476 KB |
Output is correct |
10 |
Correct |
0 ms |
14476 KB |
Output is correct |
11 |
Correct |
3 ms |
14628 KB |
Output is correct |
12 |
Correct |
0 ms |
14476 KB |
Output is correct |
13 |
Correct |
0 ms |
14476 KB |
Output is correct |
14 |
Correct |
0 ms |
14608 KB |
Output is correct |
15 |
Correct |
0 ms |
14608 KB |
Output is correct |
16 |
Correct |
0 ms |
14476 KB |
Output is correct |
17 |
Correct |
4 ms |
14776 KB |
Output is correct |
18 |
Correct |
4 ms |
14608 KB |
Output is correct |
19 |
Correct |
4 ms |
14632 KB |
Output is correct |
20 |
Correct |
7 ms |
14628 KB |
Output is correct |
21 |
Correct |
0 ms |
14476 KB |
Output is correct |
22 |
Correct |
3 ms |
14608 KB |
Output is correct |
23 |
Correct |
3 ms |
14608 KB |
Output is correct |
24 |
Correct |
6 ms |
14760 KB |
Output is correct |
25 |
Correct |
9 ms |
14768 KB |
Output is correct |
26 |
Correct |
7 ms |
14744 KB |
Output is correct |
27 |
Correct |
7 ms |
14608 KB |
Output is correct |
28 |
Correct |
8 ms |
15048 KB |
Output is correct |
29 |
Correct |
13 ms |
15840 KB |
Output is correct |
30 |
Correct |
9 ms |
14944 KB |
Output is correct |
31 |
Correct |
10 ms |
15300 KB |
Output is correct |
32 |
Correct |
6 ms |
15044 KB |
Output is correct |
33 |
Correct |
18 ms |
16808 KB |
Output is correct |
34 |
Correct |
17 ms |
16836 KB |
Output is correct |
35 |
Correct |
20 ms |
17672 KB |
Output is correct |
36 |
Correct |
0 ms |
15004 KB |
Output is correct |
37 |
Correct |
43 ms |
18708 KB |
Output is correct |
38 |
Correct |
53 ms |
18936 KB |
Output is correct |
39 |
Correct |
42 ms |
18820 KB |
Output is correct |
40 |
Correct |
57 ms |
18812 KB |
Output is correct |
41 |
Correct |
43 ms |
18804 KB |
Output is correct |
42 |
Correct |
13 ms |
15132 KB |
Output is correct |
43 |
Correct |
11 ms |
15088 KB |
Output is correct |
44 |
Correct |
14 ms |
15084 KB |
Output is correct |
45 |
Correct |
98 ms |
24148 KB |
Output is correct |
46 |
Correct |
78 ms |
24152 KB |
Output is correct |
47 |
Correct |
159 ms |
32300 KB |
Output is correct |
48 |
Correct |
95 ms |
20932 KB |
Output is correct |
49 |
Correct |
103 ms |
19196 KB |
Output is correct |
50 |
Correct |
108 ms |
19028 KB |
Output is correct |
51 |
Correct |
153 ms |
23440 KB |
Output is correct |
52 |
Correct |
162 ms |
24388 KB |
Output is correct |
53 |
Correct |
114 ms |
19228 KB |
Output is correct |
54 |
Correct |
83 ms |
16796 KB |
Output is correct |
55 |
Correct |
90 ms |
17368 KB |
Output is correct |
56 |
Correct |
99 ms |
18084 KB |
Output is correct |
57 |
Correct |
149 ms |
29668 KB |
Output is correct |
58 |
Correct |
100 ms |
18020 KB |
Output is correct |
59 |
Correct |
108 ms |
18452 KB |
Output is correct |
60 |
Correct |
111 ms |
19736 KB |
Output is correct |
61 |
Correct |
115 ms |
18312 KB |
Output is correct |
62 |
Correct |
200 ms |
27124 KB |
Output is correct |
63 |
Correct |
561 ms |
67380 KB |
Output is correct |
64 |
Correct |
646 ms |
73068 KB |
Output is correct |
65 |
Correct |
915 ms |
107236 KB |
Output is correct |
66 |
Execution timed out |
1000 ms |
143176 KB |
Program timed out |
67 |
Halted |
0 ms |
0 KB |
- |