#include <bits/stdc++.h>
#define all(x) x.begin(),x.end()
#define pii pair<int, int>
#define ft first
#define sd second
#define MP make_pair
#define PB push_back
#define sz(x) ((int)x.size())
using namespace std;
typedef long long ll;
const ll PW = 43;
const int N = 30010;
const int B = 100;
const int oo = 2e9;
set<pii> st;
vector<int> g[N];
int n, m, bg, dst[N][B], ed;
int make_nom(int x, int y){
return y * n + x;
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
// freopen("in.txt","r",stdin);
cin >> n >> m;
for (int i = 0; i < m; i++){
int ps, vl;
cin >> ps >> vl;
if (i == 0)
bg = ps;
if (i == 1)
ed = ps;
g[ps].PB(vl);
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < B; j++)
dst[i][j] = oo;
sort(all(g[i]));
g[i].resize(unique(all(g[i])) - g[i].begin());
}
dst[bg][0] = 0;
st.insert(MP(0, bg));
while (sz(st)){
int cur = (*st.begin()).sd;
st.erase(st.begin());
if (cur < n){
for (int cr : g[cur]){
if (cr >= B){
int steps = dst[cur][0];
for (int i = cur; i >= 0; i -= cr, steps++){
if (steps < dst[i][0]){
st.erase(MP(dst[i][0], i));
dst[i][0] = steps;
st.insert(MP(dst[i][0], i));
}
}
steps = dst[cur][0];
for (int i = cur; i < n; i += cr, steps++){
if (steps < dst[i][0]){
st.erase(MP(dst[i][0], i));
dst[i][0] = steps;
st.insert(MP(dst[i][0], i));
}
}
} else {
if (dst[cur][cr] > dst[cur][0]){
int nom = make_nom(cur, cr);
st.erase(MP(dst[cur][cr], nom));
dst[cur][cr] = dst[cur][0];
st.insert(MP(dst[cur][cr], nom));
}
}
}
} else {
int dv = cur / n;
cur %= n;
int nw = cur - dv;
int nd = dst[cur][dv] + 1;
if (nw >= 0){
if (dst[nw][dv] > nd){
int nom = make_nom(nw, dv);
st.erase(MP(dst[nw][dv], nom));
dst[nw][dv] = nd;
st.insert(MP(dst[nw][dv], nom));
}
if (dst[nw][0] > nd){
st.erase(MP(dst[nw][0], nw));
dst[nw][0] = nd;
st.insert(MP(dst[nw][0], nw));
}
}
nw = cur + dv;
if (nw < n){
if (dst[nw][dv] > nd){
int nom = make_nom(nw, dv);
st.erase(MP(dst[nw][dv], nom));
dst[nw][dv] = nd;
st.insert(MP(dst[nw][dv], nom));
}
if (dst[nw][0] > nd){
st.erase(MP(dst[nw][0], nw));
dst[nw][0] = nd;
st.insert(MP(dst[nw][0], nw));
}
}
}
}
cout << (dst[ed][0] == oo ? -1 : dst[ed][0]);
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
1016 KB |
Output is correct |
2 |
Correct |
3 ms |
1016 KB |
Output is correct |
3 |
Correct |
2 ms |
1016 KB |
Output is correct |
4 |
Correct |
2 ms |
1016 KB |
Output is correct |
5 |
Correct |
2 ms |
1016 KB |
Output is correct |
6 |
Correct |
3 ms |
1016 KB |
Output is correct |
7 |
Correct |
2 ms |
1016 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
1016 KB |
Output is correct |
2 |
Correct |
2 ms |
1016 KB |
Output is correct |
3 |
Correct |
2 ms |
1016 KB |
Output is correct |
4 |
Correct |
3 ms |
1016 KB |
Output is correct |
5 |
Correct |
2 ms |
1016 KB |
Output is correct |
6 |
Correct |
2 ms |
1016 KB |
Output is correct |
7 |
Correct |
2 ms |
1016 KB |
Output is correct |
8 |
Correct |
2 ms |
1020 KB |
Output is correct |
9 |
Correct |
3 ms |
1144 KB |
Output is correct |
10 |
Correct |
3 ms |
1144 KB |
Output is correct |
11 |
Correct |
4 ms |
1144 KB |
Output is correct |
12 |
Correct |
3 ms |
1144 KB |
Output is correct |
13 |
Correct |
3 ms |
1144 KB |
Output is correct |
14 |
Correct |
4 ms |
1272 KB |
Output is correct |
15 |
Correct |
4 ms |
1272 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
1016 KB |
Output is correct |
2 |
Correct |
2 ms |
1016 KB |
Output is correct |
3 |
Correct |
2 ms |
1016 KB |
Output is correct |
4 |
Correct |
3 ms |
1016 KB |
Output is correct |
5 |
Correct |
3 ms |
1016 KB |
Output is correct |
6 |
Correct |
2 ms |
1016 KB |
Output is correct |
7 |
Correct |
3 ms |
1016 KB |
Output is correct |
8 |
Correct |
2 ms |
1016 KB |
Output is correct |
9 |
Correct |
3 ms |
1016 KB |
Output is correct |
10 |
Correct |
3 ms |
1144 KB |
Output is correct |
11 |
Correct |
4 ms |
1144 KB |
Output is correct |
12 |
Correct |
3 ms |
1020 KB |
Output is correct |
13 |
Correct |
3 ms |
1016 KB |
Output is correct |
14 |
Correct |
4 ms |
1272 KB |
Output is correct |
15 |
Correct |
4 ms |
1272 KB |
Output is correct |
16 |
Correct |
3 ms |
1148 KB |
Output is correct |
17 |
Correct |
5 ms |
1400 KB |
Output is correct |
18 |
Correct |
3 ms |
1784 KB |
Output is correct |
19 |
Correct |
4 ms |
1916 KB |
Output is correct |
20 |
Correct |
4 ms |
1912 KB |
Output is correct |
21 |
Correct |
3 ms |
1272 KB |
Output is correct |
22 |
Correct |
3 ms |
1784 KB |
Output is correct |
23 |
Correct |
5 ms |
1656 KB |
Output is correct |
24 |
Correct |
6 ms |
1916 KB |
Output is correct |
25 |
Correct |
5 ms |
1916 KB |
Output is correct |
26 |
Correct |
4 ms |
1912 KB |
Output is correct |
27 |
Correct |
4 ms |
1784 KB |
Output is correct |
28 |
Correct |
5 ms |
1912 KB |
Output is correct |
29 |
Correct |
14 ms |
1912 KB |
Output is correct |
30 |
Correct |
6 ms |
1912 KB |
Output is correct |
31 |
Correct |
9 ms |
1912 KB |
Output is correct |
32 |
Correct |
7 ms |
1784 KB |
Output is correct |
33 |
Correct |
23 ms |
1912 KB |
Output is correct |
34 |
Correct |
24 ms |
1912 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
1016 KB |
Output is correct |
2 |
Correct |
2 ms |
1016 KB |
Output is correct |
3 |
Correct |
2 ms |
1016 KB |
Output is correct |
4 |
Correct |
2 ms |
1016 KB |
Output is correct |
5 |
Correct |
3 ms |
1016 KB |
Output is correct |
6 |
Correct |
3 ms |
1044 KB |
Output is correct |
7 |
Correct |
2 ms |
1016 KB |
Output is correct |
8 |
Correct |
2 ms |
1016 KB |
Output is correct |
9 |
Correct |
2 ms |
1144 KB |
Output is correct |
10 |
Correct |
3 ms |
1144 KB |
Output is correct |
11 |
Correct |
4 ms |
1144 KB |
Output is correct |
12 |
Correct |
3 ms |
1016 KB |
Output is correct |
13 |
Correct |
3 ms |
1148 KB |
Output is correct |
14 |
Correct |
4 ms |
1148 KB |
Output is correct |
15 |
Correct |
4 ms |
1144 KB |
Output is correct |
16 |
Correct |
3 ms |
1144 KB |
Output is correct |
17 |
Correct |
5 ms |
1400 KB |
Output is correct |
18 |
Correct |
3 ms |
1784 KB |
Output is correct |
19 |
Correct |
3 ms |
1784 KB |
Output is correct |
20 |
Correct |
4 ms |
1912 KB |
Output is correct |
21 |
Correct |
3 ms |
1144 KB |
Output is correct |
22 |
Correct |
3 ms |
1784 KB |
Output is correct |
23 |
Correct |
4 ms |
1656 KB |
Output is correct |
24 |
Correct |
5 ms |
1784 KB |
Output is correct |
25 |
Correct |
4 ms |
1912 KB |
Output is correct |
26 |
Correct |
4 ms |
1784 KB |
Output is correct |
27 |
Correct |
4 ms |
1784 KB |
Output is correct |
28 |
Correct |
5 ms |
1912 KB |
Output is correct |
29 |
Correct |
13 ms |
1924 KB |
Output is correct |
30 |
Correct |
6 ms |
1784 KB |
Output is correct |
31 |
Correct |
9 ms |
1912 KB |
Output is correct |
32 |
Correct |
8 ms |
1784 KB |
Output is correct |
33 |
Correct |
24 ms |
1912 KB |
Output is correct |
34 |
Correct |
24 ms |
1912 KB |
Output is correct |
35 |
Correct |
23 ms |
1912 KB |
Output is correct |
36 |
Correct |
6 ms |
1528 KB |
Output is correct |
37 |
Correct |
28 ms |
2168 KB |
Output is correct |
38 |
Correct |
30 ms |
2168 KB |
Output is correct |
39 |
Correct |
29 ms |
2168 KB |
Output is correct |
40 |
Correct |
27 ms |
2168 KB |
Output is correct |
41 |
Correct |
27 ms |
2140 KB |
Output is correct |
42 |
Correct |
9 ms |
2040 KB |
Output is correct |
43 |
Correct |
9 ms |
2044 KB |
Output is correct |
44 |
Correct |
9 ms |
2040 KB |
Output is correct |
45 |
Correct |
46 ms |
2296 KB |
Output is correct |
46 |
Correct |
46 ms |
2296 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
1016 KB |
Output is correct |
2 |
Correct |
3 ms |
1016 KB |
Output is correct |
3 |
Correct |
2 ms |
1016 KB |
Output is correct |
4 |
Correct |
2 ms |
1016 KB |
Output is correct |
5 |
Correct |
3 ms |
1016 KB |
Output is correct |
6 |
Correct |
2 ms |
1016 KB |
Output is correct |
7 |
Correct |
2 ms |
1016 KB |
Output is correct |
8 |
Correct |
3 ms |
1016 KB |
Output is correct |
9 |
Correct |
2 ms |
1144 KB |
Output is correct |
10 |
Correct |
3 ms |
1144 KB |
Output is correct |
11 |
Correct |
4 ms |
1144 KB |
Output is correct |
12 |
Correct |
3 ms |
1144 KB |
Output is correct |
13 |
Correct |
3 ms |
1144 KB |
Output is correct |
14 |
Correct |
4 ms |
1144 KB |
Output is correct |
15 |
Correct |
4 ms |
1144 KB |
Output is correct |
16 |
Correct |
3 ms |
1144 KB |
Output is correct |
17 |
Correct |
5 ms |
1400 KB |
Output is correct |
18 |
Correct |
3 ms |
1784 KB |
Output is correct |
19 |
Correct |
3 ms |
1784 KB |
Output is correct |
20 |
Correct |
4 ms |
1912 KB |
Output is correct |
21 |
Correct |
3 ms |
1144 KB |
Output is correct |
22 |
Correct |
3 ms |
1784 KB |
Output is correct |
23 |
Correct |
4 ms |
1784 KB |
Output is correct |
24 |
Correct |
5 ms |
1916 KB |
Output is correct |
25 |
Correct |
4 ms |
1912 KB |
Output is correct |
26 |
Correct |
4 ms |
1912 KB |
Output is correct |
27 |
Correct |
4 ms |
1912 KB |
Output is correct |
28 |
Correct |
5 ms |
1912 KB |
Output is correct |
29 |
Correct |
13 ms |
1912 KB |
Output is correct |
30 |
Correct |
6 ms |
1784 KB |
Output is correct |
31 |
Correct |
9 ms |
1912 KB |
Output is correct |
32 |
Correct |
8 ms |
1912 KB |
Output is correct |
33 |
Correct |
24 ms |
1912 KB |
Output is correct |
34 |
Correct |
24 ms |
1916 KB |
Output is correct |
35 |
Correct |
20 ms |
1916 KB |
Output is correct |
36 |
Correct |
6 ms |
1528 KB |
Output is correct |
37 |
Correct |
28 ms |
2168 KB |
Output is correct |
38 |
Correct |
27 ms |
2168 KB |
Output is correct |
39 |
Correct |
27 ms |
2168 KB |
Output is correct |
40 |
Correct |
27 ms |
2168 KB |
Output is correct |
41 |
Correct |
27 ms |
2168 KB |
Output is correct |
42 |
Correct |
9 ms |
2040 KB |
Output is correct |
43 |
Correct |
9 ms |
2040 KB |
Output is correct |
44 |
Correct |
9 ms |
2040 KB |
Output is correct |
45 |
Correct |
46 ms |
2296 KB |
Output is correct |
46 |
Correct |
48 ms |
2296 KB |
Output is correct |
47 |
Correct |
105 ms |
6776 KB |
Output is correct |
48 |
Correct |
18 ms |
10616 KB |
Output is correct |
49 |
Correct |
18 ms |
11384 KB |
Output is correct |
50 |
Correct |
17 ms |
12408 KB |
Output is correct |
51 |
Correct |
77 ms |
14072 KB |
Output is correct |
52 |
Correct |
80 ms |
14072 KB |
Output is correct |
53 |
Correct |
32 ms |
13432 KB |
Output is correct |
54 |
Correct |
14 ms |
12792 KB |
Output is correct |
55 |
Correct |
16 ms |
12792 KB |
Output is correct |
56 |
Correct |
27 ms |
13688 KB |
Output is correct |
57 |
Correct |
123 ms |
12936 KB |
Output is correct |
58 |
Correct |
25 ms |
13048 KB |
Output is correct |
59 |
Correct |
30 ms |
13048 KB |
Output is correct |
60 |
Correct |
32 ms |
13048 KB |
Output is correct |
61 |
Correct |
31 ms |
13040 KB |
Output is correct |
62 |
Correct |
55 ms |
13688 KB |
Output is correct |
63 |
Correct |
200 ms |
14456 KB |
Output is correct |
64 |
Correct |
108 ms |
14456 KB |
Output is correct |
65 |
Correct |
136 ms |
14328 KB |
Output is correct |
66 |
Correct |
683 ms |
14628 KB |
Output is correct |
67 |
Correct |
717 ms |
14712 KB |
Output is correct |