#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
const int INF = 0x3F3F3F3F;
const int MAX = 3e4 + 5;
int N, M, A, B;
vector<int> arr[MAX];
int dist[MAX];
bool vis[MAX];
int dijk() {
fill(dist, dist + N + 1, INF);
priority_queue< pii, vector<pii>, greater<pii> > pq;
dist[A] = 0;
pq.push({dist[A], A});
while (!pq.empty()) {
pii x = pq.top();
pq.pop();
int u = x.second;
if (!vis[u]) {
vis[u] = true;
for (int i : arr[u]) {
int v;
for (int j = 1; u + (i * j) < N; j++) {
v = u + (i * j);
if (dist[u] + j < dist[v]) {
dist[v] = dist[u] + j;
pq.push({dist[v], v});
}
}
for (int j = -1; u + (i * j) >= 0; j--) {
v = u + (i * j);
if (dist[u] + abs(j) < dist[v]) {
dist[v] = dist[u] + abs(j);
pq.push({dist[v], v});
}
}
}
}
}
return dist[B];
}
int main() {
cin >> N >> M;
int a, b;
for (int i = 0; i < M; i++) {
cin >> a >> b;
arr[a].push_back(b);
if (i == 0) A = a;
else if (i == 1) B = a;
}
int ans = dijk();
printf("%d", ans == INF ? -1 : ans);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
2 ms |
912 KB |
Output is correct |
3 |
Correct |
1 ms |
1012 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 |
1016 KB |
Output is correct |
7 |
Correct |
2 ms |
980 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1016 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 |
1020 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
2 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 |
2 ms |
980 KB |
Output is correct |
11 |
Correct |
2 ms |
980 KB |
Output is correct |
12 |
Correct |
2 ms |
980 KB |
Output is correct |
13 |
Correct |
2 ms |
1020 KB |
Output is correct |
14 |
Correct |
2 ms |
1028 KB |
Output is correct |
15 |
Correct |
2 ms |
1024 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1012 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
1012 KB |
Output is correct |
4 |
Correct |
2 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
1016 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
8 |
Correct |
2 ms |
980 KB |
Output is correct |
9 |
Correct |
2 ms |
980 KB |
Output is correct |
10 |
Correct |
2 ms |
1024 KB |
Output is correct |
11 |
Correct |
1 ms |
980 KB |
Output is correct |
12 |
Correct |
2 ms |
1024 KB |
Output is correct |
13 |
Correct |
2 ms |
980 KB |
Output is correct |
14 |
Correct |
2 ms |
980 KB |
Output is correct |
15 |
Correct |
3 ms |
980 KB |
Output is correct |
16 |
Correct |
2 ms |
980 KB |
Output is correct |
17 |
Correct |
2 ms |
980 KB |
Output is correct |
18 |
Correct |
2 ms |
980 KB |
Output is correct |
19 |
Correct |
1 ms |
980 KB |
Output is correct |
20 |
Correct |
8 ms |
1032 KB |
Output is correct |
21 |
Correct |
2 ms |
980 KB |
Output is correct |
22 |
Correct |
1 ms |
1020 KB |
Output is correct |
23 |
Correct |
2 ms |
980 KB |
Output is correct |
24 |
Correct |
3 ms |
1028 KB |
Output is correct |
25 |
Correct |
2 ms |
1108 KB |
Output is correct |
26 |
Correct |
4 ms |
980 KB |
Output is correct |
27 |
Correct |
5 ms |
1032 KB |
Output is correct |
28 |
Correct |
4 ms |
1032 KB |
Output is correct |
29 |
Correct |
5 ms |
1236 KB |
Output is correct |
30 |
Correct |
2 ms |
1096 KB |
Output is correct |
31 |
Correct |
2 ms |
1108 KB |
Output is correct |
32 |
Correct |
2 ms |
1108 KB |
Output is correct |
33 |
Correct |
5 ms |
1280 KB |
Output is correct |
34 |
Correct |
5 ms |
1284 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
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 |
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 |
980 KB |
Output is correct |
11 |
Correct |
2 ms |
980 KB |
Output is correct |
12 |
Correct |
2 ms |
980 KB |
Output is correct |
13 |
Correct |
3 ms |
980 KB |
Output is correct |
14 |
Correct |
2 ms |
980 KB |
Output is correct |
15 |
Correct |
2 ms |
980 KB |
Output is correct |
16 |
Correct |
2 ms |
980 KB |
Output is correct |
17 |
Correct |
2 ms |
980 KB |
Output is correct |
18 |
Correct |
1 ms |
1016 KB |
Output is correct |
19 |
Correct |
1 ms |
980 KB |
Output is correct |
20 |
Correct |
5 ms |
1108 KB |
Output is correct |
21 |
Correct |
2 ms |
1020 KB |
Output is correct |
22 |
Correct |
1 ms |
980 KB |
Output is correct |
23 |
Correct |
2 ms |
1024 KB |
Output is correct |
24 |
Correct |
3 ms |
1108 KB |
Output is correct |
25 |
Correct |
3 ms |
1108 KB |
Output is correct |
26 |
Correct |
4 ms |
980 KB |
Output is correct |
27 |
Correct |
5 ms |
1108 KB |
Output is correct |
28 |
Correct |
2 ms |
1108 KB |
Output is correct |
29 |
Correct |
3 ms |
1236 KB |
Output is correct |
30 |
Correct |
2 ms |
1108 KB |
Output is correct |
31 |
Correct |
2 ms |
1108 KB |
Output is correct |
32 |
Correct |
2 ms |
1108 KB |
Output is correct |
33 |
Correct |
3 ms |
1236 KB |
Output is correct |
34 |
Correct |
4 ms |
1236 KB |
Output is correct |
35 |
Correct |
20 ms |
1480 KB |
Output is correct |
36 |
Correct |
4 ms |
1028 KB |
Output is correct |
37 |
Correct |
10 ms |
1364 KB |
Output is correct |
38 |
Correct |
15 ms |
1620 KB |
Output is correct |
39 |
Correct |
17 ms |
1540 KB |
Output is correct |
40 |
Correct |
18 ms |
1536 KB |
Output is correct |
41 |
Correct |
17 ms |
1540 KB |
Output is correct |
42 |
Correct |
49 ms |
1400 KB |
Output is correct |
43 |
Correct |
53 ms |
1436 KB |
Output is correct |
44 |
Correct |
52 ms |
1476 KB |
Output is correct |
45 |
Correct |
15 ms |
1620 KB |
Output is correct |
46 |
Correct |
15 ms |
1620 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1012 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 |
1016 KB |
Output is correct |
7 |
Correct |
1 ms |
936 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 |
980 KB |
Output is correct |
11 |
Correct |
2 ms |
1024 KB |
Output is correct |
12 |
Correct |
2 ms |
980 KB |
Output is correct |
13 |
Correct |
2 ms |
1020 KB |
Output is correct |
14 |
Correct |
2 ms |
980 KB |
Output is correct |
15 |
Correct |
2 ms |
980 KB |
Output is correct |
16 |
Correct |
1 ms |
1020 KB |
Output is correct |
17 |
Correct |
2 ms |
980 KB |
Output is correct |
18 |
Correct |
2 ms |
980 KB |
Output is correct |
19 |
Correct |
1 ms |
980 KB |
Output is correct |
20 |
Correct |
6 ms |
1108 KB |
Output is correct |
21 |
Correct |
2 ms |
980 KB |
Output is correct |
22 |
Correct |
1 ms |
1016 KB |
Output is correct |
23 |
Correct |
2 ms |
980 KB |
Output is correct |
24 |
Correct |
3 ms |
1108 KB |
Output is correct |
25 |
Correct |
2 ms |
1108 KB |
Output is correct |
26 |
Correct |
5 ms |
980 KB |
Output is correct |
27 |
Correct |
6 ms |
980 KB |
Output is correct |
28 |
Correct |
2 ms |
1108 KB |
Output is correct |
29 |
Correct |
3 ms |
1236 KB |
Output is correct |
30 |
Correct |
2 ms |
1108 KB |
Output is correct |
31 |
Correct |
2 ms |
1108 KB |
Output is correct |
32 |
Correct |
2 ms |
1108 KB |
Output is correct |
33 |
Correct |
4 ms |
1236 KB |
Output is correct |
34 |
Correct |
4 ms |
1236 KB |
Output is correct |
35 |
Correct |
14 ms |
1420 KB |
Output is correct |
36 |
Correct |
4 ms |
1108 KB |
Output is correct |
37 |
Correct |
13 ms |
1456 KB |
Output is correct |
38 |
Correct |
16 ms |
1548 KB |
Output is correct |
39 |
Correct |
21 ms |
1612 KB |
Output is correct |
40 |
Correct |
19 ms |
1620 KB |
Output is correct |
41 |
Correct |
16 ms |
1632 KB |
Output is correct |
42 |
Correct |
50 ms |
1468 KB |
Output is correct |
43 |
Correct |
56 ms |
1492 KB |
Output is correct |
44 |
Correct |
50 ms |
1364 KB |
Output is correct |
45 |
Correct |
14 ms |
1536 KB |
Output is correct |
46 |
Correct |
14 ms |
1620 KB |
Output is correct |
47 |
Correct |
22 ms |
2180 KB |
Output is correct |
48 |
Correct |
14 ms |
1796 KB |
Output is correct |
49 |
Correct |
13 ms |
1748 KB |
Output is correct |
50 |
Correct |
10 ms |
1620 KB |
Output is correct |
51 |
Correct |
44 ms |
3148 KB |
Output is correct |
52 |
Correct |
35 ms |
2636 KB |
Output is correct |
53 |
Correct |
30 ms |
2648 KB |
Output is correct |
54 |
Correct |
2 ms |
1408 KB |
Output is correct |
55 |
Correct |
2 ms |
1396 KB |
Output is correct |
56 |
Correct |
622 ms |
2652 KB |
Output is correct |
57 |
Correct |
44 ms |
3408 KB |
Output is correct |
58 |
Correct |
650 ms |
1628 KB |
Output is correct |
59 |
Correct |
584 ms |
1760 KB |
Output is correct |
60 |
Correct |
630 ms |
1732 KB |
Output is correct |
61 |
Correct |
544 ms |
1780 KB |
Output is correct |
62 |
Correct |
19 ms |
2300 KB |
Output is correct |
63 |
Correct |
39 ms |
2860 KB |
Output is correct |
64 |
Correct |
38 ms |
3676 KB |
Output is correct |
65 |
Correct |
41 ms |
3712 KB |
Output is correct |
66 |
Correct |
58 ms |
3608 KB |
Output is correct |
67 |
Correct |
60 ms |
3660 KB |
Output is correct |