#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx2")
using namespace std;
#define int long long
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n , m;
cin>>n>>m;
int sz = (int)sqrt(n);
vector<vector<int>> p(n);
vector<vector<int>> dist(n , vector<int>(sz +1 , LLONG_MAX));
vector<vector<bool>> vis(n , vector<bool>(sz + 1 , 0));
priority_queue<tuple<int ,int , int>, vector<tuple<int ,int , int>> , greater<tuple<int,int ,int>>> q;
int trg = 0;
for(int i = 0 ; i < m; i++)
{
int a , b;
cin>>a>>b;
if(i == 0)
{
dist[a][0] = 0;
q.push({0 , a , 0});
}
if(i == 1)
{
trg = a;
}
p[a].push_back(b);
}
while(!q.empty())
{
auto [d , tp , last] = q.top();
q.pop();
if(vis[tp][last])
continue;
vis[tp][last] = 1;
vector<int> all = p[tp];
all.push_back(last);
for(auto P : all)
{
if(P == 0)
continue;
if(P > sz)
{
for(int k = 1 ; tp + k * P < n ; k++)
{
if(dist[k * P + tp][0] > dist[tp][last] + k)
{
dist[k * P + tp][0] = dist[tp][last] + k;
q.push({dist[k * P + tp][0] , k * P + tp , 0});
}
}
for(int k = 1 ; tp - k * P >= 0 ; k++)
{
if(dist[tp - k * P][0] > dist[tp][last] + k)
{
dist[tp - k * P][0] = dist[tp][last] + k;
q.push({dist[tp - k * P][0] , tp - k * P , 0});
}
}
}
else
{
if(P + tp < n)
{
if(dist[P + tp][P] > dist[tp][last] + 1)
{
dist[P + tp][P] = dist[tp][last] + 1;
q.push({dist[P + tp][P] , P + tp , P});
}
}
if(tp - P >= 0)
{
if(dist[tp - P][P] > dist[tp][last] + 1)
{
dist[tp - P][P] = dist[tp][last] + 1;
q.push({dist[tp - P][P] , tp - P , P});
}
}
}
}
}
int mn = LLONG_MAX;
for(int i = 0 ; i <= sz ; i++)
{
mn = min(mn , dist[trg][i]);
}
cout<<(mn == LLONG_MAX ? -1 : mn);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
452 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
476 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
2 ms |
604 KB |
Output is correct |
18 |
Correct |
1 ms |
1116 KB |
Output is correct |
19 |
Correct |
1 ms |
1368 KB |
Output is correct |
20 |
Correct |
1 ms |
1372 KB |
Output is correct |
21 |
Correct |
1 ms |
604 KB |
Output is correct |
22 |
Correct |
1 ms |
1116 KB |
Output is correct |
23 |
Correct |
2 ms |
1212 KB |
Output is correct |
24 |
Correct |
3 ms |
1372 KB |
Output is correct |
25 |
Correct |
2 ms |
1336 KB |
Output is correct |
26 |
Correct |
2 ms |
1488 KB |
Output is correct |
27 |
Correct |
1 ms |
1372 KB |
Output is correct |
28 |
Correct |
2 ms |
1492 KB |
Output is correct |
29 |
Correct |
7 ms |
1368 KB |
Output is correct |
30 |
Correct |
3 ms |
1372 KB |
Output is correct |
31 |
Correct |
5 ms |
1372 KB |
Output is correct |
32 |
Correct |
2 ms |
1372 KB |
Output is correct |
33 |
Correct |
16 ms |
1628 KB |
Output is correct |
34 |
Correct |
13 ms |
1628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
456 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
2 ms |
604 KB |
Output is correct |
18 |
Correct |
1 ms |
1116 KB |
Output is correct |
19 |
Correct |
1 ms |
1372 KB |
Output is correct |
20 |
Correct |
1 ms |
1372 KB |
Output is correct |
21 |
Correct |
1 ms |
604 KB |
Output is correct |
22 |
Correct |
1 ms |
1116 KB |
Output is correct |
23 |
Correct |
2 ms |
1116 KB |
Output is correct |
24 |
Correct |
2 ms |
1372 KB |
Output is correct |
25 |
Correct |
2 ms |
1372 KB |
Output is correct |
26 |
Correct |
2 ms |
1372 KB |
Output is correct |
27 |
Correct |
1 ms |
1372 KB |
Output is correct |
28 |
Correct |
2 ms |
1372 KB |
Output is correct |
29 |
Correct |
7 ms |
1372 KB |
Output is correct |
30 |
Correct |
2 ms |
1372 KB |
Output is correct |
31 |
Correct |
4 ms |
1372 KB |
Output is correct |
32 |
Correct |
3 ms |
1624 KB |
Output is correct |
33 |
Correct |
13 ms |
1628 KB |
Output is correct |
34 |
Correct |
14 ms |
1628 KB |
Output is correct |
35 |
Correct |
18 ms |
1828 KB |
Output is correct |
36 |
Correct |
3 ms |
860 KB |
Output is correct |
37 |
Correct |
22 ms |
2020 KB |
Output is correct |
38 |
Correct |
23 ms |
2396 KB |
Output is correct |
39 |
Correct |
24 ms |
2076 KB |
Output is correct |
40 |
Correct |
23 ms |
2136 KB |
Output is correct |
41 |
Correct |
22 ms |
2304 KB |
Output is correct |
42 |
Correct |
5 ms |
2312 KB |
Output is correct |
43 |
Correct |
4 ms |
2312 KB |
Output is correct |
44 |
Correct |
4 ms |
2252 KB |
Output is correct |
45 |
Correct |
41 ms |
2324 KB |
Output is correct |
46 |
Correct |
41 ms |
2328 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
2 ms |
604 KB |
Output is correct |
18 |
Correct |
1 ms |
1116 KB |
Output is correct |
19 |
Correct |
1 ms |
1372 KB |
Output is correct |
20 |
Correct |
1 ms |
1492 KB |
Output is correct |
21 |
Correct |
0 ms |
604 KB |
Output is correct |
22 |
Correct |
1 ms |
1116 KB |
Output is correct |
23 |
Correct |
2 ms |
1116 KB |
Output is correct |
24 |
Correct |
3 ms |
1368 KB |
Output is correct |
25 |
Correct |
2 ms |
1372 KB |
Output is correct |
26 |
Correct |
3 ms |
1492 KB |
Output is correct |
27 |
Correct |
1 ms |
1372 KB |
Output is correct |
28 |
Correct |
2 ms |
1372 KB |
Output is correct |
29 |
Correct |
6 ms |
1372 KB |
Output is correct |
30 |
Correct |
2 ms |
1372 KB |
Output is correct |
31 |
Correct |
5 ms |
1372 KB |
Output is correct |
32 |
Correct |
2 ms |
1372 KB |
Output is correct |
33 |
Correct |
13 ms |
1628 KB |
Output is correct |
34 |
Correct |
13 ms |
1624 KB |
Output is correct |
35 |
Correct |
14 ms |
1628 KB |
Output is correct |
36 |
Correct |
4 ms |
860 KB |
Output is correct |
37 |
Correct |
22 ms |
2024 KB |
Output is correct |
38 |
Correct |
24 ms |
2392 KB |
Output is correct |
39 |
Correct |
22 ms |
2304 KB |
Output is correct |
40 |
Correct |
22 ms |
2140 KB |
Output is correct |
41 |
Correct |
22 ms |
2308 KB |
Output is correct |
42 |
Correct |
4 ms |
2312 KB |
Output is correct |
43 |
Correct |
4 ms |
2480 KB |
Output is correct |
44 |
Correct |
4 ms |
2304 KB |
Output is correct |
45 |
Correct |
41 ms |
2328 KB |
Output is correct |
46 |
Correct |
41 ms |
2328 KB |
Output is correct |
47 |
Correct |
131 ms |
14424 KB |
Output is correct |
48 |
Correct |
18 ms |
31832 KB |
Output is correct |
49 |
Correct |
19 ms |
36188 KB |
Output is correct |
50 |
Correct |
21 ms |
41308 KB |
Output is correct |
51 |
Correct |
78 ms |
46924 KB |
Output is correct |
52 |
Correct |
78 ms |
47192 KB |
Output is correct |
53 |
Correct |
33 ms |
46240 KB |
Output is correct |
54 |
Correct |
27 ms |
45144 KB |
Output is correct |
55 |
Correct |
21 ms |
45144 KB |
Output is correct |
56 |
Correct |
27 ms |
46424 KB |
Output is correct |
57 |
Correct |
96 ms |
45404 KB |
Output is correct |
58 |
Correct |
27 ms |
46160 KB |
Output is correct |
59 |
Correct |
28 ms |
46180 KB |
Output is correct |
60 |
Correct |
36 ms |
46180 KB |
Output is correct |
61 |
Correct |
32 ms |
46248 KB |
Output is correct |
62 |
Correct |
63 ms |
46428 KB |
Output is correct |
63 |
Correct |
68 ms |
49864 KB |
Output is correct |
64 |
Correct |
61 ms |
49096 KB |
Output is correct |
65 |
Correct |
791 ms |
49096 KB |
Output is correct |
66 |
Execution timed out |
1043 ms |
53184 KB |
Time limit exceeded |
67 |
Halted |
0 ms |
0 KB |
- |