#include <bits/stdc++.h>
using namespace std;
const int MAX = (3e4)+1, INF = 1e9;
int n, m, s, e, a[MAX], b[MAX], d[MAX];
vector<int> ls[MAX];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
for(int i = 0; i < n; i++) d[i] = INF;
for(int i = 0; i < m; i++) {
cin >> a[i] >> b[i];
ls[a[i]].push_back(b[i]);
if(i == 0) s = a[i];
if(i == 1) e = a[i];
}
for(int i = 0; i < n; i++) {
sort(ls[i].begin(), ls[i].end());
ls[i].resize(distance(ls[i].begin(), unique(ls[i].begin(), ls[i].end())));
}
priority_queue<pair<int,int>, vector<pair<int,int> >, greater<pair<int,int> > > pq;
pq.emplace(d[s] = 0, s);
while(!pq.empty()) {
int dd, u; tie(dd, u) = pq.top();
pq.pop();
if(d[u] < dd) continue;
for(int p: ls[u]) {
for(int i = 1; u + i*p < n; i++) {
if(d[u] + i < d[u + i*p]) pq.emplace(d[u + i*p] = d[u] + i, u + i*p);
if(binary_search(ls[u + i*p].begin(), ls[u + i*p].end(), p)) break;
}
for(int i = 1; u - i*p >= 0; i++) {
if(d[u] + i < d[u - i*p]) pq.emplace(d[u - i*p] = d[u] + i, u - i*p);
if(binary_search(ls[u - i*p].begin(), ls[u - i*p].end(), p)) break;
}
}
}
cout << (d[e] == INF ? -1 : d[e]);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
3236 KB |
Output is correct |
2 |
Correct |
0 ms |
3236 KB |
Output is correct |
3 |
Correct |
0 ms |
3236 KB |
Output is correct |
4 |
Correct |
0 ms |
3236 KB |
Output is correct |
5 |
Correct |
0 ms |
3236 KB |
Output is correct |
6 |
Correct |
0 ms |
3236 KB |
Output is correct |
7 |
Correct |
0 ms |
3236 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
3236 KB |
Output is correct |
2 |
Correct |
0 ms |
3236 KB |
Output is correct |
3 |
Correct |
0 ms |
3236 KB |
Output is correct |
4 |
Correct |
0 ms |
3236 KB |
Output is correct |
5 |
Correct |
0 ms |
3236 KB |
Output is correct |
6 |
Correct |
0 ms |
3236 KB |
Output is correct |
7 |
Correct |
0 ms |
3236 KB |
Output is correct |
8 |
Correct |
0 ms |
3236 KB |
Output is correct |
9 |
Correct |
0 ms |
3236 KB |
Output is correct |
10 |
Correct |
0 ms |
3236 KB |
Output is correct |
11 |
Correct |
0 ms |
3236 KB |
Output is correct |
12 |
Correct |
0 ms |
3236 KB |
Output is correct |
13 |
Correct |
0 ms |
3236 KB |
Output is correct |
14 |
Correct |
0 ms |
3236 KB |
Output is correct |
15 |
Correct |
0 ms |
3236 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
3236 KB |
Output is correct |
2 |
Correct |
0 ms |
3236 KB |
Output is correct |
3 |
Correct |
0 ms |
3236 KB |
Output is correct |
4 |
Correct |
0 ms |
3236 KB |
Output is correct |
5 |
Correct |
0 ms |
3236 KB |
Output is correct |
6 |
Correct |
0 ms |
3236 KB |
Output is correct |
7 |
Correct |
0 ms |
3236 KB |
Output is correct |
8 |
Correct |
0 ms |
3236 KB |
Output is correct |
9 |
Correct |
0 ms |
3236 KB |
Output is correct |
10 |
Correct |
0 ms |
3236 KB |
Output is correct |
11 |
Correct |
0 ms |
3236 KB |
Output is correct |
12 |
Correct |
0 ms |
3236 KB |
Output is correct |
13 |
Correct |
0 ms |
3236 KB |
Output is correct |
14 |
Correct |
0 ms |
3236 KB |
Output is correct |
15 |
Correct |
0 ms |
3236 KB |
Output is correct |
16 |
Correct |
0 ms |
3236 KB |
Output is correct |
17 |
Correct |
0 ms |
3236 KB |
Output is correct |
18 |
Correct |
0 ms |
3236 KB |
Output is correct |
19 |
Correct |
0 ms |
3236 KB |
Output is correct |
20 |
Correct |
0 ms |
3236 KB |
Output is correct |
21 |
Correct |
0 ms |
3236 KB |
Output is correct |
22 |
Correct |
0 ms |
3236 KB |
Output is correct |
23 |
Correct |
0 ms |
3236 KB |
Output is correct |
24 |
Correct |
0 ms |
3236 KB |
Output is correct |
25 |
Correct |
0 ms |
3372 KB |
Output is correct |
26 |
Correct |
0 ms |
3236 KB |
Output is correct |
27 |
Correct |
0 ms |
3236 KB |
Output is correct |
28 |
Correct |
0 ms |
3236 KB |
Output is correct |
29 |
Correct |
3 ms |
3404 KB |
Output is correct |
30 |
Correct |
0 ms |
3400 KB |
Output is correct |
31 |
Correct |
0 ms |
3400 KB |
Output is correct |
32 |
Correct |
0 ms |
3404 KB |
Output is correct |
33 |
Correct |
3 ms |
3672 KB |
Output is correct |
34 |
Correct |
3 ms |
3672 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
3236 KB |
Output is correct |
2 |
Correct |
0 ms |
3236 KB |
Output is correct |
3 |
Correct |
0 ms |
3236 KB |
Output is correct |
4 |
Correct |
0 ms |
3236 KB |
Output is correct |
5 |
Correct |
0 ms |
3236 KB |
Output is correct |
6 |
Correct |
0 ms |
3236 KB |
Output is correct |
7 |
Correct |
0 ms |
3236 KB |
Output is correct |
8 |
Correct |
0 ms |
3236 KB |
Output is correct |
9 |
Correct |
0 ms |
3236 KB |
Output is correct |
10 |
Correct |
0 ms |
3236 KB |
Output is correct |
11 |
Correct |
0 ms |
3236 KB |
Output is correct |
12 |
Correct |
0 ms |
3236 KB |
Output is correct |
13 |
Correct |
0 ms |
3236 KB |
Output is correct |
14 |
Correct |
0 ms |
3236 KB |
Output is correct |
15 |
Correct |
0 ms |
3236 KB |
Output is correct |
16 |
Correct |
0 ms |
3236 KB |
Output is correct |
17 |
Correct |
0 ms |
3236 KB |
Output is correct |
18 |
Correct |
0 ms |
3236 KB |
Output is correct |
19 |
Correct |
0 ms |
3236 KB |
Output is correct |
20 |
Correct |
0 ms |
3236 KB |
Output is correct |
21 |
Correct |
0 ms |
3236 KB |
Output is correct |
22 |
Correct |
0 ms |
3236 KB |
Output is correct |
23 |
Correct |
0 ms |
3236 KB |
Output is correct |
24 |
Correct |
0 ms |
3236 KB |
Output is correct |
25 |
Correct |
0 ms |
3372 KB |
Output is correct |
26 |
Correct |
0 ms |
3236 KB |
Output is correct |
27 |
Correct |
0 ms |
3236 KB |
Output is correct |
28 |
Correct |
0 ms |
3236 KB |
Output is correct |
29 |
Correct |
3 ms |
3404 KB |
Output is correct |
30 |
Correct |
0 ms |
3400 KB |
Output is correct |
31 |
Correct |
0 ms |
3400 KB |
Output is correct |
32 |
Correct |
0 ms |
3404 KB |
Output is correct |
33 |
Correct |
3 ms |
3672 KB |
Output is correct |
34 |
Correct |
3 ms |
3672 KB |
Output is correct |
35 |
Correct |
9 ms |
3500 KB |
Output is correct |
36 |
Correct |
3 ms |
3236 KB |
Output is correct |
37 |
Correct |
6 ms |
3520 KB |
Output is correct |
38 |
Correct |
13 ms |
3500 KB |
Output is correct |
39 |
Correct |
13 ms |
3500 KB |
Output is correct |
40 |
Correct |
13 ms |
3500 KB |
Output is correct |
41 |
Correct |
13 ms |
3500 KB |
Output is correct |
42 |
Correct |
6 ms |
3400 KB |
Output is correct |
43 |
Correct |
6 ms |
3400 KB |
Output is correct |
44 |
Correct |
9 ms |
3396 KB |
Output is correct |
45 |
Correct |
13 ms |
3840 KB |
Output is correct |
46 |
Correct |
13 ms |
3836 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
3236 KB |
Output is correct |
2 |
Correct |
0 ms |
3236 KB |
Output is correct |
3 |
Correct |
0 ms |
3236 KB |
Output is correct |
4 |
Correct |
0 ms |
3236 KB |
Output is correct |
5 |
Correct |
0 ms |
3236 KB |
Output is correct |
6 |
Correct |
0 ms |
3236 KB |
Output is correct |
7 |
Correct |
0 ms |
3236 KB |
Output is correct |
8 |
Correct |
0 ms |
3236 KB |
Output is correct |
9 |
Correct |
0 ms |
3236 KB |
Output is correct |
10 |
Correct |
0 ms |
3236 KB |
Output is correct |
11 |
Correct |
0 ms |
3236 KB |
Output is correct |
12 |
Correct |
0 ms |
3236 KB |
Output is correct |
13 |
Correct |
0 ms |
3236 KB |
Output is correct |
14 |
Correct |
0 ms |
3236 KB |
Output is correct |
15 |
Correct |
0 ms |
3236 KB |
Output is correct |
16 |
Correct |
0 ms |
3236 KB |
Output is correct |
17 |
Correct |
0 ms |
3236 KB |
Output is correct |
18 |
Correct |
0 ms |
3236 KB |
Output is correct |
19 |
Correct |
0 ms |
3236 KB |
Output is correct |
20 |
Correct |
0 ms |
3236 KB |
Output is correct |
21 |
Correct |
0 ms |
3236 KB |
Output is correct |
22 |
Correct |
0 ms |
3236 KB |
Output is correct |
23 |
Correct |
0 ms |
3236 KB |
Output is correct |
24 |
Correct |
0 ms |
3236 KB |
Output is correct |
25 |
Correct |
0 ms |
3372 KB |
Output is correct |
26 |
Correct |
0 ms |
3236 KB |
Output is correct |
27 |
Correct |
0 ms |
3236 KB |
Output is correct |
28 |
Correct |
0 ms |
3236 KB |
Output is correct |
29 |
Correct |
3 ms |
3404 KB |
Output is correct |
30 |
Correct |
0 ms |
3400 KB |
Output is correct |
31 |
Correct |
0 ms |
3400 KB |
Output is correct |
32 |
Correct |
0 ms |
3404 KB |
Output is correct |
33 |
Correct |
3 ms |
3672 KB |
Output is correct |
34 |
Correct |
6 ms |
3672 KB |
Output is correct |
35 |
Correct |
6 ms |
3500 KB |
Output is correct |
36 |
Correct |
0 ms |
3236 KB |
Output is correct |
37 |
Correct |
9 ms |
3520 KB |
Output is correct |
38 |
Correct |
9 ms |
3500 KB |
Output is correct |
39 |
Correct |
13 ms |
3500 KB |
Output is correct |
40 |
Correct |
13 ms |
3500 KB |
Output is correct |
41 |
Correct |
9 ms |
3500 KB |
Output is correct |
42 |
Correct |
6 ms |
3400 KB |
Output is correct |
43 |
Correct |
6 ms |
3400 KB |
Output is correct |
44 |
Correct |
6 ms |
3396 KB |
Output is correct |
45 |
Correct |
13 ms |
3840 KB |
Output is correct |
46 |
Correct |
13 ms |
3836 KB |
Output is correct |
47 |
Correct |
43 ms |
4388 KB |
Output is correct |
48 |
Correct |
9 ms |
3632 KB |
Output is correct |
49 |
Correct |
9 ms |
3632 KB |
Output is correct |
50 |
Correct |
3 ms |
3500 KB |
Output is correct |
51 |
Correct |
36 ms |
4636 KB |
Output is correct |
52 |
Correct |
36 ms |
4640 KB |
Output is correct |
53 |
Correct |
23 ms |
4640 KB |
Output is correct |
54 |
Correct |
3 ms |
3396 KB |
Output is correct |
55 |
Correct |
3 ms |
3396 KB |
Output is correct |
56 |
Correct |
6 ms |
4160 KB |
Output is correct |
57 |
Correct |
26 ms |
4112 KB |
Output is correct |
58 |
Correct |
6 ms |
3528 KB |
Output is correct |
59 |
Correct |
6 ms |
3528 KB |
Output is correct |
60 |
Correct |
6 ms |
3536 KB |
Output is correct |
61 |
Correct |
9 ms |
3544 KB |
Output is correct |
62 |
Correct |
19 ms |
4160 KB |
Output is correct |
63 |
Correct |
49 ms |
6576 KB |
Output is correct |
64 |
Correct |
59 ms |
6552 KB |
Output is correct |
65 |
Correct |
76 ms |
6568 KB |
Output is correct |
66 |
Correct |
133 ms |
9596 KB |
Output is correct |
67 |
Correct |
143 ms |
9596 KB |
Output is correct |