#include <bits/stdc++.h>
#define int long long
#define INF 1000000000000000
#define lchild(i) (i*2 + 1)
#define rchild(i) (i*2 + 2)
#define mid(l, u) ((l+u)/2)
#define x(p) p.first
#define y(p) p.second
#define MOD 998244353
using namespace std;
bool ez[30000][30000];
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, m;
cin>>n>>m;
int s = 0, t= 0;
vector<int> adj[n];
for(int i = 0;i<m;i++){
int b, p;
cin>>b>>p;
if(!ez[b][p]) adj[b].push_back(p);
ez[b][p] = true;
if(i==0) s = b;
if(i==1) t = b;
}
int dist[n];
for(int i = 0;i<n;i++) dist[i] = INF;
dist[s] = 0;
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
pq.push({0, s});
while(!pq.empty()){
pair<int, int> curr = pq.top();
pq.pop();
if(curr.first != dist[curr.second]) continue;
for(int j: adj[curr.second]){
int len = 0;
for(int i = curr.second + j;i<n;i+=j){
len++;
if(curr.first + len < dist[i]){
dist[i] = curr.first + len;
pq.push({dist[i], i});
}
if(ez[i][j]) break;
}
len = 0;
for(int i = curr.second - j;i>=0;i-=j){
len++;
if(curr.first + len < dist[i]){
dist[i] = curr.first + len;
pq.push({dist[i], i});
}
if(ez[i][j]) break;
}
}
}
if(dist[t]==INF) dist[t] = -1;
cout<<dist[t]<<"\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
4 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
384 KB |
Output is correct |
8 |
Correct |
4 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
512 KB |
Output is correct |
10 |
Correct |
5 ms |
768 KB |
Output is correct |
11 |
Correct |
6 ms |
768 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
768 KB |
Output is correct |
14 |
Correct |
5 ms |
640 KB |
Output is correct |
15 |
Correct |
5 ms |
640 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
4 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
512 KB |
Output is correct |
10 |
Correct |
6 ms |
768 KB |
Output is correct |
11 |
Correct |
6 ms |
768 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
768 KB |
Output is correct |
14 |
Correct |
5 ms |
640 KB |
Output is correct |
15 |
Correct |
5 ms |
640 KB |
Output is correct |
16 |
Correct |
6 ms |
1152 KB |
Output is correct |
17 |
Correct |
8 ms |
3328 KB |
Output is correct |
18 |
Correct |
7 ms |
3712 KB |
Output is correct |
19 |
Correct |
6 ms |
2560 KB |
Output is correct |
20 |
Correct |
12 ms |
8576 KB |
Output is correct |
21 |
Correct |
6 ms |
2176 KB |
Output is correct |
22 |
Correct |
6 ms |
2944 KB |
Output is correct |
23 |
Correct |
8 ms |
3584 KB |
Output is correct |
24 |
Correct |
10 ms |
5760 KB |
Output is correct |
25 |
Correct |
10 ms |
6016 KB |
Output is correct |
26 |
Correct |
6 ms |
896 KB |
Output is correct |
27 |
Correct |
6 ms |
768 KB |
Output is correct |
28 |
Correct |
11 ms |
8576 KB |
Output is correct |
29 |
Correct |
7 ms |
1024 KB |
Output is correct |
30 |
Correct |
7 ms |
768 KB |
Output is correct |
31 |
Correct |
7 ms |
896 KB |
Output is correct |
32 |
Correct |
7 ms |
896 KB |
Output is correct |
33 |
Correct |
8 ms |
1156 KB |
Output is correct |
34 |
Correct |
9 ms |
1200 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
288 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
4 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
416 KB |
Output is correct |
9 |
Correct |
4 ms |
512 KB |
Output is correct |
10 |
Correct |
5 ms |
768 KB |
Output is correct |
11 |
Correct |
5 ms |
768 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
896 KB |
Output is correct |
14 |
Correct |
5 ms |
768 KB |
Output is correct |
15 |
Correct |
5 ms |
640 KB |
Output is correct |
16 |
Correct |
6 ms |
1152 KB |
Output is correct |
17 |
Correct |
8 ms |
3328 KB |
Output is correct |
18 |
Correct |
7 ms |
3712 KB |
Output is correct |
19 |
Correct |
6 ms |
2560 KB |
Output is correct |
20 |
Correct |
11 ms |
8576 KB |
Output is correct |
21 |
Correct |
6 ms |
2176 KB |
Output is correct |
22 |
Correct |
6 ms |
2944 KB |
Output is correct |
23 |
Correct |
8 ms |
3584 KB |
Output is correct |
24 |
Correct |
10 ms |
5760 KB |
Output is correct |
25 |
Correct |
10 ms |
6016 KB |
Output is correct |
26 |
Correct |
6 ms |
896 KB |
Output is correct |
27 |
Correct |
6 ms |
768 KB |
Output is correct |
28 |
Correct |
12 ms |
8576 KB |
Output is correct |
29 |
Correct |
8 ms |
1024 KB |
Output is correct |
30 |
Correct |
7 ms |
768 KB |
Output is correct |
31 |
Correct |
7 ms |
896 KB |
Output is correct |
32 |
Correct |
7 ms |
896 KB |
Output is correct |
33 |
Correct |
8 ms |
1156 KB |
Output is correct |
34 |
Correct |
9 ms |
1152 KB |
Output is correct |
35 |
Correct |
18 ms |
9088 KB |
Output is correct |
36 |
Correct |
10 ms |
6016 KB |
Output is correct |
37 |
Correct |
18 ms |
10112 KB |
Output is correct |
38 |
Correct |
23 ms |
12544 KB |
Output is correct |
39 |
Correct |
25 ms |
12544 KB |
Output is correct |
40 |
Correct |
23 ms |
12544 KB |
Output is correct |
41 |
Correct |
24 ms |
12544 KB |
Output is correct |
42 |
Correct |
9 ms |
896 KB |
Output is correct |
43 |
Correct |
9 ms |
768 KB |
Output is correct |
44 |
Correct |
14 ms |
8576 KB |
Output is correct |
45 |
Correct |
14 ms |
2304 KB |
Output is correct |
46 |
Correct |
14 ms |
2304 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
384 KB |
Output is correct |
8 |
Correct |
4 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
512 KB |
Output is correct |
10 |
Correct |
5 ms |
768 KB |
Output is correct |
11 |
Correct |
5 ms |
768 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
768 KB |
Output is correct |
14 |
Correct |
5 ms |
640 KB |
Output is correct |
15 |
Correct |
5 ms |
640 KB |
Output is correct |
16 |
Correct |
5 ms |
1152 KB |
Output is correct |
17 |
Correct |
7 ms |
3328 KB |
Output is correct |
18 |
Correct |
7 ms |
3712 KB |
Output is correct |
19 |
Correct |
6 ms |
2560 KB |
Output is correct |
20 |
Correct |
11 ms |
8576 KB |
Output is correct |
21 |
Correct |
6 ms |
2176 KB |
Output is correct |
22 |
Correct |
6 ms |
2944 KB |
Output is correct |
23 |
Correct |
8 ms |
3584 KB |
Output is correct |
24 |
Correct |
10 ms |
5760 KB |
Output is correct |
25 |
Correct |
10 ms |
6016 KB |
Output is correct |
26 |
Correct |
6 ms |
896 KB |
Output is correct |
27 |
Correct |
6 ms |
896 KB |
Output is correct |
28 |
Correct |
11 ms |
8576 KB |
Output is correct |
29 |
Correct |
8 ms |
1024 KB |
Output is correct |
30 |
Correct |
7 ms |
768 KB |
Output is correct |
31 |
Correct |
7 ms |
896 KB |
Output is correct |
32 |
Correct |
7 ms |
896 KB |
Output is correct |
33 |
Correct |
8 ms |
1156 KB |
Output is correct |
34 |
Correct |
9 ms |
1152 KB |
Output is correct |
35 |
Correct |
18 ms |
9088 KB |
Output is correct |
36 |
Correct |
10 ms |
6016 KB |
Output is correct |
37 |
Correct |
18 ms |
10240 KB |
Output is correct |
38 |
Correct |
23 ms |
12544 KB |
Output is correct |
39 |
Correct |
23 ms |
12544 KB |
Output is correct |
40 |
Correct |
23 ms |
12544 KB |
Output is correct |
41 |
Correct |
23 ms |
12416 KB |
Output is correct |
42 |
Correct |
9 ms |
896 KB |
Output is correct |
43 |
Correct |
10 ms |
768 KB |
Output is correct |
44 |
Correct |
14 ms |
8576 KB |
Output is correct |
45 |
Correct |
15 ms |
2304 KB |
Output is correct |
46 |
Correct |
14 ms |
2304 KB |
Output is correct |
47 |
Correct |
78 ms |
53364 KB |
Output is correct |
48 |
Correct |
74 ms |
85756 KB |
Output is correct |
49 |
Correct |
74 ms |
83148 KB |
Output is correct |
50 |
Correct |
53 ms |
60280 KB |
Output is correct |
51 |
Correct |
156 ms |
116852 KB |
Output is correct |
52 |
Correct |
164 ms |
116916 KB |
Output is correct |
53 |
Correct |
111 ms |
95904 KB |
Output is correct |
54 |
Correct |
15 ms |
3456 KB |
Output is correct |
55 |
Correct |
19 ms |
3328 KB |
Output is correct |
56 |
Correct |
103 ms |
123896 KB |
Output is correct |
57 |
Correct |
59 ms |
13168 KB |
Output is correct |
58 |
Correct |
29 ms |
3456 KB |
Output is correct |
59 |
Correct |
32 ms |
3456 KB |
Output is correct |
60 |
Correct |
37 ms |
4224 KB |
Output is correct |
61 |
Correct |
37 ms |
4988 KB |
Output is correct |
62 |
Correct |
128 ms |
123768 KB |
Output is correct |
63 |
Correct |
87 ms |
8180 KB |
Output is correct |
64 |
Correct |
94 ms |
7664 KB |
Output is correct |
65 |
Correct |
116 ms |
9328 KB |
Output is correct |
66 |
Correct |
169 ms |
8816 KB |
Output is correct |
67 |
Correct |
176 ms |
8808 KB |
Output is correct |