#include <bits/stdc++.h>
#define pb push_back
#define F first
#define S second
#define debug(x) cout << #x << "= " << x << ", "
#define ll long long
#define fast ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define SZ(x) (int) x.size()
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int maxn = 3e4 + 10 , maxsq = 175 , inf = 1e9 + 10;
int n , b[maxn] , p[maxn] , m , dis[maxn][maxsq + 10] , ans;
bool marked[maxn][maxsq + 10];
vector <int> all[maxn];
priority_queue <pair <int , pair <int , int>>> st;
void add(int v , int j , int val)
{
if(dis[v][j] > val)
{
dis[v][j] = val;
st.push({-1 * dis[v][j] , {v , j}});
}
}
void jmp_small(int D , int now , int jmp)
{
if(now + jmp < n)
add(now + jmp , jmp , D + 1);
if(now - jmp > -1)
add(now - jmp , jmp , D + 1);
}
void jmp_big(int D , int now , int jmp)
{
for(int tmp = now + jmp ; tmp < n ; tmp += jmp)
add(tmp , maxsq , D + ((tmp - now) / jmp));
for(int tmp = now - jmp ; tmp > -1 ; tmp -= jmp)
add(tmp , maxsq , D + ((now - tmp) / jmp));
}
void update(int D , int now , int jmp)
{
if(marked[now][jmp])
return;
marked[now][jmp] = true;
if(jmp != maxsq)
{
jmp_small(D , now , jmp);
return;
}
for(auto u : all[now])
{
if(u < maxsq)
jmp_small(D , now , u);
else
jmp_big(D , now , u);
}
}
void dij()
{
for(int i = 0 ; i < n ; i++)
for(int j = 0 ; j <= maxsq ; j++)
dis[i][j] = inf;
dis[b[0]][maxsq] = 0;
st.push({0 , {b[0] , maxsq}});
while(!st.empty())
{
auto v = st.top();
st.pop();
int D = v.F * -1 , now = v.S.F , jmp = v.S.S;
if(now == b[1])
{
ans = D;
break;
}
if(!marked[now][maxsq])
{
jmp = maxsq;
update(D , now , jmp);
}
jmp = v.S.S;
update(D , now , jmp);
}
}
int32_t main()
{
fast;
cin >> n >> m;
ans = inf;
for(int i = 0 ; i < m ; i++)
{
cin >> b[i] >> p[i];
all[b[i]].pb(p[i]);
}
dij();
cout << (ans == inf ? -1 : ans) << endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
8 |
Correct |
1 ms |
980 KB |
Output is correct |
9 |
Correct |
1 ms |
980 KB |
Output is correct |
10 |
Correct |
1 ms |
1108 KB |
Output is correct |
11 |
Correct |
1 ms |
1108 KB |
Output is correct |
12 |
Correct |
1 ms |
1108 KB |
Output is correct |
13 |
Correct |
1 ms |
1108 KB |
Output is correct |
14 |
Correct |
2 ms |
1236 KB |
Output is correct |
15 |
Correct |
1 ms |
1108 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
8 |
Correct |
1 ms |
980 KB |
Output is correct |
9 |
Correct |
1 ms |
980 KB |
Output is correct |
10 |
Correct |
1 ms |
1048 KB |
Output is correct |
11 |
Correct |
1 ms |
1108 KB |
Output is correct |
12 |
Correct |
1 ms |
1108 KB |
Output is correct |
13 |
Correct |
1 ms |
1108 KB |
Output is correct |
14 |
Correct |
2 ms |
1236 KB |
Output is correct |
15 |
Correct |
1 ms |
1108 KB |
Output is correct |
16 |
Correct |
1 ms |
1108 KB |
Output is correct |
17 |
Correct |
2 ms |
1748 KB |
Output is correct |
18 |
Correct |
1 ms |
2260 KB |
Output is correct |
19 |
Correct |
1 ms |
2516 KB |
Output is correct |
20 |
Correct |
2 ms |
2900 KB |
Output is correct |
21 |
Correct |
1 ms |
1336 KB |
Output is correct |
22 |
Correct |
1 ms |
2260 KB |
Output is correct |
23 |
Correct |
2 ms |
2516 KB |
Output is correct |
24 |
Correct |
4 ms |
2772 KB |
Output is correct |
25 |
Correct |
2 ms |
2816 KB |
Output is correct |
26 |
Correct |
2 ms |
2900 KB |
Output is correct |
27 |
Correct |
2 ms |
2772 KB |
Output is correct |
28 |
Correct |
3 ms |
2900 KB |
Output is correct |
29 |
Correct |
8 ms |
2900 KB |
Output is correct |
30 |
Correct |
3 ms |
2772 KB |
Output is correct |
31 |
Correct |
7 ms |
2772 KB |
Output is correct |
32 |
Correct |
4 ms |
2772 KB |
Output is correct |
33 |
Correct |
15 ms |
2996 KB |
Output is correct |
34 |
Correct |
9 ms |
2900 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
8 |
Correct |
1 ms |
980 KB |
Output is correct |
9 |
Correct |
1 ms |
980 KB |
Output is correct |
10 |
Correct |
1 ms |
1108 KB |
Output is correct |
11 |
Correct |
1 ms |
1108 KB |
Output is correct |
12 |
Correct |
1 ms |
1108 KB |
Output is correct |
13 |
Correct |
1 ms |
1108 KB |
Output is correct |
14 |
Correct |
2 ms |
1236 KB |
Output is correct |
15 |
Correct |
1 ms |
1108 KB |
Output is correct |
16 |
Correct |
1 ms |
1108 KB |
Output is correct |
17 |
Correct |
2 ms |
1748 KB |
Output is correct |
18 |
Correct |
1 ms |
2260 KB |
Output is correct |
19 |
Correct |
1 ms |
2516 KB |
Output is correct |
20 |
Correct |
2 ms |
2900 KB |
Output is correct |
21 |
Correct |
1 ms |
1364 KB |
Output is correct |
22 |
Correct |
1 ms |
2260 KB |
Output is correct |
23 |
Correct |
2 ms |
2528 KB |
Output is correct |
24 |
Correct |
3 ms |
2772 KB |
Output is correct |
25 |
Correct |
2 ms |
2840 KB |
Output is correct |
26 |
Correct |
3 ms |
2900 KB |
Output is correct |
27 |
Correct |
2 ms |
2772 KB |
Output is correct |
28 |
Correct |
3 ms |
2900 KB |
Output is correct |
29 |
Correct |
7 ms |
2900 KB |
Output is correct |
30 |
Correct |
3 ms |
2772 KB |
Output is correct |
31 |
Correct |
5 ms |
2772 KB |
Output is correct |
32 |
Correct |
4 ms |
2772 KB |
Output is correct |
33 |
Correct |
15 ms |
2900 KB |
Output is correct |
34 |
Correct |
10 ms |
2900 KB |
Output is correct |
35 |
Correct |
11 ms |
2868 KB |
Output is correct |
36 |
Correct |
2 ms |
2144 KB |
Output is correct |
37 |
Correct |
5 ms |
3228 KB |
Output is correct |
38 |
Correct |
7 ms |
3416 KB |
Output is correct |
39 |
Correct |
6 ms |
3028 KB |
Output is correct |
40 |
Correct |
6 ms |
3304 KB |
Output is correct |
41 |
Correct |
7 ms |
3412 KB |
Output is correct |
42 |
Correct |
5 ms |
3320 KB |
Output is correct |
43 |
Correct |
7 ms |
3288 KB |
Output is correct |
44 |
Correct |
5 ms |
3284 KB |
Output is correct |
45 |
Correct |
51 ms |
3788 KB |
Output is correct |
46 |
Correct |
25 ms |
3752 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
984 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
8 |
Correct |
1 ms |
980 KB |
Output is correct |
9 |
Correct |
1 ms |
980 KB |
Output is correct |
10 |
Correct |
1 ms |
1108 KB |
Output is correct |
11 |
Correct |
1 ms |
1108 KB |
Output is correct |
12 |
Correct |
1 ms |
1108 KB |
Output is correct |
13 |
Correct |
1 ms |
1108 KB |
Output is correct |
14 |
Correct |
3 ms |
1236 KB |
Output is correct |
15 |
Correct |
1 ms |
1108 KB |
Output is correct |
16 |
Correct |
1 ms |
1108 KB |
Output is correct |
17 |
Correct |
2 ms |
1748 KB |
Output is correct |
18 |
Correct |
2 ms |
2260 KB |
Output is correct |
19 |
Correct |
2 ms |
2516 KB |
Output is correct |
20 |
Correct |
2 ms |
2928 KB |
Output is correct |
21 |
Correct |
1 ms |
1364 KB |
Output is correct |
22 |
Correct |
1 ms |
2260 KB |
Output is correct |
23 |
Correct |
3 ms |
2516 KB |
Output is correct |
24 |
Correct |
3 ms |
2772 KB |
Output is correct |
25 |
Correct |
2 ms |
2900 KB |
Output is correct |
26 |
Correct |
2 ms |
2900 KB |
Output is correct |
27 |
Correct |
2 ms |
2772 KB |
Output is correct |
28 |
Correct |
3 ms |
2900 KB |
Output is correct |
29 |
Correct |
8 ms |
2900 KB |
Output is correct |
30 |
Correct |
3 ms |
2772 KB |
Output is correct |
31 |
Correct |
6 ms |
2900 KB |
Output is correct |
32 |
Correct |
4 ms |
2772 KB |
Output is correct |
33 |
Correct |
14 ms |
2980 KB |
Output is correct |
34 |
Correct |
9 ms |
2928 KB |
Output is correct |
35 |
Correct |
6 ms |
2924 KB |
Output is correct |
36 |
Correct |
2 ms |
2132 KB |
Output is correct |
37 |
Correct |
5 ms |
3284 KB |
Output is correct |
38 |
Correct |
8 ms |
3412 KB |
Output is correct |
39 |
Correct |
6 ms |
3028 KB |
Output is correct |
40 |
Correct |
6 ms |
3284 KB |
Output is correct |
41 |
Correct |
8 ms |
3412 KB |
Output is correct |
42 |
Correct |
8 ms |
3296 KB |
Output is correct |
43 |
Correct |
7 ms |
3284 KB |
Output is correct |
44 |
Correct |
6 ms |
3284 KB |
Output is correct |
45 |
Correct |
55 ms |
3788 KB |
Output is correct |
46 |
Correct |
27 ms |
3724 KB |
Output is correct |
47 |
Correct |
13 ms |
12752 KB |
Output is correct |
48 |
Correct |
14 ms |
18508 KB |
Output is correct |
49 |
Correct |
14 ms |
20052 KB |
Output is correct |
50 |
Correct |
15 ms |
21716 KB |
Output is correct |
51 |
Correct |
28 ms |
29460 KB |
Output is correct |
52 |
Correct |
31 ms |
29484 KB |
Output is correct |
53 |
Correct |
22 ms |
25112 KB |
Output is correct |
54 |
Correct |
15 ms |
28088 KB |
Output is correct |
55 |
Correct |
17 ms |
28104 KB |
Output is correct |
56 |
Correct |
22 ms |
29252 KB |
Output is correct |
57 |
Correct |
12 ms |
23036 KB |
Output is correct |
58 |
Correct |
22 ms |
28660 KB |
Output is correct |
59 |
Correct |
21 ms |
28660 KB |
Output is correct |
60 |
Correct |
22 ms |
28624 KB |
Output is correct |
61 |
Correct |
21 ms |
28688 KB |
Output is correct |
62 |
Correct |
47 ms |
29336 KB |
Output is correct |
63 |
Correct |
50 ms |
29480 KB |
Output is correct |
64 |
Correct |
61 ms |
29436 KB |
Output is correct |
65 |
Correct |
305 ms |
29568 KB |
Output is correct |
66 |
Correct |
720 ms |
29828 KB |
Output is correct |
67 |
Correct |
346 ms |
29764 KB |
Output is correct |