#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
#define pii pair<int, int>
const int MX = 3e5 + 5;
int INF;
vector<int> gr[MX];
int b[MX], p[MX];
int d[MX], vi[MX];
int n, m;
// ------------ FUNCTIONS -----------
void bfs(){
memset(d, 63, sizeof d);
INF = d[0];
priority_queue<pii> q;
q.push({0, b[1]});
d[b[1]] = 0;
while(q.size()){
int w, u;
tie(w, u) = q.top();
w = -w;
q.pop();
if(w != d[u]){
continue;
}
for(int pw : gr[u]){
for(int i = u + pw, c = 1; i < n; i += pw, ++c){
if(d[u] + c < d[i]){
d[i] = d[u] + c;
q.push({-d[i], i});
}
}
for(int i = u - pw, c = 1; i >= 0; i -= pw, ++c){
if(d[u] + c < d[i]){
d[i] = d[u] + c;
q.push({-d[i], i});
}
}
}
}
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
//freopen("main.inp","r",stdin);
//freopen("main.out","w",stdout);
cin>>n>>m;
for(int i=1; i<=m; ++i){
cin>>b[i]>>p[i];
gr[b[i]].push_back(p[i]);
}
bfs();
cout<<(d[b[2]] == INF ? -1 : d[b[2]]);
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
14424 KB |
Output is correct |
2 |
Correct |
3 ms |
14428 KB |
Output is correct |
3 |
Correct |
3 ms |
14428 KB |
Output is correct |
4 |
Correct |
3 ms |
14516 KB |
Output is correct |
5 |
Correct |
3 ms |
14428 KB |
Output is correct |
6 |
Correct |
3 ms |
14428 KB |
Output is correct |
7 |
Correct |
3 ms |
14660 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
14428 KB |
Output is correct |
2 |
Correct |
3 ms |
14428 KB |
Output is correct |
3 |
Correct |
3 ms |
14428 KB |
Output is correct |
4 |
Correct |
3 ms |
14428 KB |
Output is correct |
5 |
Correct |
3 ms |
14664 KB |
Output is correct |
6 |
Correct |
3 ms |
14428 KB |
Output is correct |
7 |
Correct |
3 ms |
14680 KB |
Output is correct |
8 |
Correct |
3 ms |
14428 KB |
Output is correct |
9 |
Correct |
3 ms |
14428 KB |
Output is correct |
10 |
Correct |
3 ms |
14684 KB |
Output is correct |
11 |
Correct |
3 ms |
14684 KB |
Output is correct |
12 |
Correct |
3 ms |
14684 KB |
Output is correct |
13 |
Correct |
3 ms |
14684 KB |
Output is correct |
14 |
Correct |
3 ms |
14520 KB |
Output is correct |
15 |
Correct |
4 ms |
14680 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
14428 KB |
Output is correct |
2 |
Correct |
3 ms |
14424 KB |
Output is correct |
3 |
Correct |
3 ms |
14428 KB |
Output is correct |
4 |
Correct |
3 ms |
14656 KB |
Output is correct |
5 |
Correct |
4 ms |
14428 KB |
Output is correct |
6 |
Correct |
3 ms |
14424 KB |
Output is correct |
7 |
Correct |
3 ms |
14428 KB |
Output is correct |
8 |
Correct |
3 ms |
14428 KB |
Output is correct |
9 |
Correct |
3 ms |
14428 KB |
Output is correct |
10 |
Correct |
3 ms |
14684 KB |
Output is correct |
11 |
Correct |
3 ms |
14680 KB |
Output is correct |
12 |
Correct |
4 ms |
14680 KB |
Output is correct |
13 |
Correct |
3 ms |
14660 KB |
Output is correct |
14 |
Correct |
3 ms |
14936 KB |
Output is correct |
15 |
Correct |
4 ms |
14684 KB |
Output is correct |
16 |
Correct |
3 ms |
14680 KB |
Output is correct |
17 |
Correct |
4 ms |
14684 KB |
Output is correct |
18 |
Correct |
4 ms |
14684 KB |
Output is correct |
19 |
Correct |
3 ms |
14684 KB |
Output is correct |
20 |
Correct |
6 ms |
14684 KB |
Output is correct |
21 |
Correct |
4 ms |
14660 KB |
Output is correct |
22 |
Correct |
3 ms |
14684 KB |
Output is correct |
23 |
Correct |
4 ms |
14684 KB |
Output is correct |
24 |
Correct |
5 ms |
14768 KB |
Output is correct |
25 |
Correct |
4 ms |
14684 KB |
Output is correct |
26 |
Correct |
6 ms |
14684 KB |
Output is correct |
27 |
Correct |
6 ms |
14672 KB |
Output is correct |
28 |
Correct |
4 ms |
14680 KB |
Output is correct |
29 |
Correct |
5 ms |
14684 KB |
Output is correct |
30 |
Correct |
4 ms |
14684 KB |
Output is correct |
31 |
Correct |
4 ms |
14684 KB |
Output is correct |
32 |
Correct |
4 ms |
14684 KB |
Output is correct |
33 |
Correct |
4 ms |
14940 KB |
Output is correct |
34 |
Correct |
5 ms |
14940 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
14428 KB |
Output is correct |
2 |
Correct |
3 ms |
14424 KB |
Output is correct |
3 |
Correct |
3 ms |
14428 KB |
Output is correct |
4 |
Correct |
3 ms |
14524 KB |
Output is correct |
5 |
Correct |
4 ms |
14428 KB |
Output is correct |
6 |
Correct |
3 ms |
14428 KB |
Output is correct |
7 |
Correct |
3 ms |
14428 KB |
Output is correct |
8 |
Correct |
3 ms |
14428 KB |
Output is correct |
9 |
Correct |
3 ms |
14428 KB |
Output is correct |
10 |
Correct |
4 ms |
14684 KB |
Output is correct |
11 |
Correct |
4 ms |
14684 KB |
Output is correct |
12 |
Correct |
4 ms |
14684 KB |
Output is correct |
13 |
Correct |
4 ms |
14680 KB |
Output is correct |
14 |
Correct |
4 ms |
14680 KB |
Output is correct |
15 |
Correct |
4 ms |
14680 KB |
Output is correct |
16 |
Correct |
3 ms |
14684 KB |
Output is correct |
17 |
Correct |
4 ms |
14680 KB |
Output is correct |
18 |
Correct |
3 ms |
14936 KB |
Output is correct |
19 |
Correct |
3 ms |
14492 KB |
Output is correct |
20 |
Correct |
6 ms |
14684 KB |
Output is correct |
21 |
Correct |
4 ms |
14684 KB |
Output is correct |
22 |
Correct |
4 ms |
14508 KB |
Output is correct |
23 |
Correct |
3 ms |
14684 KB |
Output is correct |
24 |
Correct |
4 ms |
14624 KB |
Output is correct |
25 |
Correct |
4 ms |
14684 KB |
Output is correct |
26 |
Correct |
5 ms |
14684 KB |
Output is correct |
27 |
Correct |
5 ms |
14600 KB |
Output is correct |
28 |
Correct |
3 ms |
14684 KB |
Output is correct |
29 |
Correct |
4 ms |
14684 KB |
Output is correct |
30 |
Correct |
4 ms |
14684 KB |
Output is correct |
31 |
Correct |
4 ms |
14940 KB |
Output is correct |
32 |
Correct |
4 ms |
14684 KB |
Output is correct |
33 |
Correct |
5 ms |
14920 KB |
Output is correct |
34 |
Correct |
5 ms |
14940 KB |
Output is correct |
35 |
Correct |
8 ms |
15264 KB |
Output is correct |
36 |
Correct |
4 ms |
14684 KB |
Output is correct |
37 |
Correct |
7 ms |
15196 KB |
Output is correct |
38 |
Correct |
8 ms |
15452 KB |
Output is correct |
39 |
Correct |
9 ms |
15452 KB |
Output is correct |
40 |
Correct |
8 ms |
15448 KB |
Output is correct |
41 |
Correct |
8 ms |
15452 KB |
Output is correct |
42 |
Correct |
41 ms |
15068 KB |
Output is correct |
43 |
Correct |
42 ms |
15064 KB |
Output is correct |
44 |
Correct |
42 ms |
15104 KB |
Output is correct |
45 |
Correct |
8 ms |
15452 KB |
Output is correct |
46 |
Correct |
8 ms |
15452 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
14424 KB |
Output is correct |
2 |
Correct |
3 ms |
14428 KB |
Output is correct |
3 |
Correct |
4 ms |
14660 KB |
Output is correct |
4 |
Correct |
3 ms |
14424 KB |
Output is correct |
5 |
Correct |
3 ms |
14428 KB |
Output is correct |
6 |
Correct |
3 ms |
14428 KB |
Output is correct |
7 |
Correct |
3 ms |
14428 KB |
Output is correct |
8 |
Correct |
3 ms |
14428 KB |
Output is correct |
9 |
Correct |
3 ms |
14428 KB |
Output is correct |
10 |
Correct |
3 ms |
14684 KB |
Output is correct |
11 |
Correct |
3 ms |
14684 KB |
Output is correct |
12 |
Correct |
3 ms |
14684 KB |
Output is correct |
13 |
Correct |
3 ms |
14684 KB |
Output is correct |
14 |
Correct |
3 ms |
14512 KB |
Output is correct |
15 |
Correct |
3 ms |
14684 KB |
Output is correct |
16 |
Correct |
3 ms |
14684 KB |
Output is correct |
17 |
Correct |
4 ms |
14684 KB |
Output is correct |
18 |
Correct |
4 ms |
14668 KB |
Output is correct |
19 |
Correct |
3 ms |
14680 KB |
Output is correct |
20 |
Correct |
6 ms |
14664 KB |
Output is correct |
21 |
Correct |
3 ms |
14684 KB |
Output is correct |
22 |
Correct |
3 ms |
14684 KB |
Output is correct |
23 |
Correct |
4 ms |
14680 KB |
Output is correct |
24 |
Correct |
5 ms |
14688 KB |
Output is correct |
25 |
Correct |
5 ms |
14684 KB |
Output is correct |
26 |
Correct |
6 ms |
14680 KB |
Output is correct |
27 |
Correct |
5 ms |
14684 KB |
Output is correct |
28 |
Correct |
3 ms |
14680 KB |
Output is correct |
29 |
Correct |
4 ms |
14680 KB |
Output is correct |
30 |
Correct |
4 ms |
14680 KB |
Output is correct |
31 |
Correct |
4 ms |
14680 KB |
Output is correct |
32 |
Correct |
4 ms |
14684 KB |
Output is correct |
33 |
Correct |
4 ms |
14940 KB |
Output is correct |
34 |
Correct |
5 ms |
14940 KB |
Output is correct |
35 |
Correct |
8 ms |
15360 KB |
Output is correct |
36 |
Correct |
4 ms |
14680 KB |
Output is correct |
37 |
Correct |
7 ms |
15192 KB |
Output is correct |
38 |
Correct |
9 ms |
15448 KB |
Output is correct |
39 |
Correct |
9 ms |
15452 KB |
Output is correct |
40 |
Correct |
9 ms |
15568 KB |
Output is correct |
41 |
Correct |
9 ms |
15348 KB |
Output is correct |
42 |
Correct |
42 ms |
15068 KB |
Output is correct |
43 |
Correct |
41 ms |
15068 KB |
Output is correct |
44 |
Correct |
41 ms |
15108 KB |
Output is correct |
45 |
Correct |
7 ms |
15448 KB |
Output is correct |
46 |
Correct |
8 ms |
15448 KB |
Output is correct |
47 |
Correct |
16 ms |
16348 KB |
Output is correct |
48 |
Correct |
7 ms |
15176 KB |
Output is correct |
49 |
Correct |
7 ms |
15300 KB |
Output is correct |
50 |
Correct |
6 ms |
15196 KB |
Output is correct |
51 |
Correct |
23 ms |
17620 KB |
Output is correct |
52 |
Correct |
22 ms |
16600 KB |
Output is correct |
53 |
Correct |
16 ms |
16600 KB |
Output is correct |
54 |
Correct |
5 ms |
14940 KB |
Output is correct |
55 |
Correct |
4 ms |
15192 KB |
Output is correct |
56 |
Correct |
579 ms |
16352 KB |
Output is correct |
57 |
Correct |
41 ms |
20168 KB |
Output is correct |
58 |
Correct |
559 ms |
15324 KB |
Output is correct |
59 |
Correct |
533 ms |
15448 KB |
Output is correct |
60 |
Correct |
549 ms |
15448 KB |
Output is correct |
61 |
Correct |
477 ms |
15444 KB |
Output is correct |
62 |
Correct |
10 ms |
15708 KB |
Output is correct |
63 |
Correct |
22 ms |
17520 KB |
Output is correct |
64 |
Correct |
22 ms |
17368 KB |
Output is correct |
65 |
Correct |
24 ms |
17360 KB |
Output is correct |
66 |
Correct |
31 ms |
20368 KB |
Output is correct |
67 |
Correct |
31 ms |
19408 KB |
Output is correct |