// In the name of the God
#include <bits/stdc++.h>
#define ll long long
// #define int long long
#define pb push_back
#define F first
#define S second
#define mp make_pair
#define pii pair <int, int>
#define smin(x, y) (x) = min((x), (y))
#define smax(x, y) (x) = max((x), (y))
#define all(x) (x).begin(), (x).end()
using namespace std;
const int inf = 1e9+7;
const int mod = 998244353;
const int maxn = 3e4+5;
int n, m, s, t, cnt = 0, dist[maxn];
set <int> doge[maxn];
set <pii> st;
void dij(int v) {
fill(dist, dist+n, inf);
dist[v] = 0;
st.insert(mp(0, v));
while (st.size()) {
int d = st.begin()->F, u = st.begin()->S;
st.erase(st.begin());
for (int p : doge[u]) {
for (int j = 1; u+j*p < n; j++) {
if (dist[u+j*p] > d+j) {
st.erase(mp(dist[u+j*p], u+j*p));
dist[u+j*p] = d+j;
st.insert(mp(d+j, u+j*p));
}
cnt++; assert(cnt < 7381230);
if (doge[u+j*p].find(p) != doge[u+j*p].end()) break;
}
for (int j = 1; u-j*p >= 0; j++) {
if (dist[u-j*p] > d+j) {
st.erase(mp(dist[u-j*p], u-j*p));
dist[u-j*p] = d+j;
st.insert(mp(d+j, u-j*p));
}
cnt++; assert(cnt < 7381230);
if (doge[u-j*p].find(p) != doge[u-j*p].end()) break;
}
}
}
}
int32_t main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> m;
for (int i = 0; i < m; i++) {
int a, b; cin >> a >> b;
doge[a].insert(b);
if (i == 0) s = a;
if (i == 1) t = a;
}
dij(s);
if (dist[t] != inf) assert(dist[t] <= 10000000);
cout << (dist[t] == inf ? -1 : dist[t]) << '\n';
// int sum = 0, i;
// for ( i = 1; sum <= maxn; i++) sum += i;
// cout << i << '\n';
// sum = 0;
// for (int j = 1; j <= i; j++) sum += maxn;
// cout << sum << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1628 KB |
Output is correct |
2 |
Correct |
1 ms |
1628 KB |
Output is correct |
3 |
Correct |
1 ms |
1628 KB |
Output is correct |
4 |
Correct |
1 ms |
1628 KB |
Output is correct |
5 |
Correct |
1 ms |
1628 KB |
Output is correct |
6 |
Correct |
1 ms |
1628 KB |
Output is correct |
7 |
Correct |
1 ms |
1628 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1628 KB |
Output is correct |
2 |
Correct |
1 ms |
1628 KB |
Output is correct |
3 |
Correct |
1 ms |
1628 KB |
Output is correct |
4 |
Correct |
2 ms |
1628 KB |
Output is correct |
5 |
Correct |
1 ms |
1628 KB |
Output is correct |
6 |
Correct |
1 ms |
1628 KB |
Output is correct |
7 |
Correct |
1 ms |
1628 KB |
Output is correct |
8 |
Correct |
1 ms |
1628 KB |
Output is correct |
9 |
Correct |
1 ms |
1628 KB |
Output is correct |
10 |
Correct |
1 ms |
1884 KB |
Output is correct |
11 |
Correct |
1 ms |
1884 KB |
Output is correct |
12 |
Correct |
1 ms |
1628 KB |
Output is correct |
13 |
Correct |
1 ms |
1628 KB |
Output is correct |
14 |
Correct |
2 ms |
1880 KB |
Output is correct |
15 |
Correct |
1 ms |
1884 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
1628 KB |
Output is correct |
2 |
Correct |
1 ms |
1628 KB |
Output is correct |
3 |
Correct |
1 ms |
1628 KB |
Output is correct |
4 |
Correct |
1 ms |
1628 KB |
Output is correct |
5 |
Correct |
1 ms |
1628 KB |
Output is correct |
6 |
Correct |
1 ms |
1628 KB |
Output is correct |
7 |
Correct |
1 ms |
1624 KB |
Output is correct |
8 |
Correct |
1 ms |
1628 KB |
Output is correct |
9 |
Correct |
1 ms |
1628 KB |
Output is correct |
10 |
Correct |
1 ms |
1884 KB |
Output is correct |
11 |
Correct |
1 ms |
1884 KB |
Output is correct |
12 |
Correct |
1 ms |
1628 KB |
Output is correct |
13 |
Correct |
1 ms |
1628 KB |
Output is correct |
14 |
Correct |
1 ms |
1884 KB |
Output is correct |
15 |
Correct |
1 ms |
1884 KB |
Output is correct |
16 |
Correct |
1 ms |
1884 KB |
Output is correct |
17 |
Correct |
2 ms |
1884 KB |
Output is correct |
18 |
Correct |
1 ms |
1884 KB |
Output is correct |
19 |
Correct |
1 ms |
1884 KB |
Output is correct |
20 |
Correct |
1 ms |
1884 KB |
Output is correct |
21 |
Correct |
1 ms |
1880 KB |
Output is correct |
22 |
Correct |
1 ms |
1880 KB |
Output is correct |
23 |
Correct |
2 ms |
1880 KB |
Output is correct |
24 |
Correct |
2 ms |
1884 KB |
Output is correct |
25 |
Correct |
2 ms |
1884 KB |
Output is correct |
26 |
Correct |
1 ms |
1884 KB |
Output is correct |
27 |
Correct |
1 ms |
1884 KB |
Output is correct |
28 |
Correct |
2 ms |
1884 KB |
Output is correct |
29 |
Correct |
4 ms |
1884 KB |
Output is correct |
30 |
Correct |
2 ms |
1880 KB |
Output is correct |
31 |
Correct |
2 ms |
1884 KB |
Output is correct |
32 |
Correct |
2 ms |
1884 KB |
Output is correct |
33 |
Correct |
6 ms |
1884 KB |
Output is correct |
34 |
Correct |
6 ms |
1884 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1628 KB |
Output is correct |
2 |
Correct |
1 ms |
1872 KB |
Output is correct |
3 |
Correct |
1 ms |
1628 KB |
Output is correct |
4 |
Correct |
1 ms |
1880 KB |
Output is correct |
5 |
Correct |
1 ms |
1624 KB |
Output is correct |
6 |
Correct |
1 ms |
1628 KB |
Output is correct |
7 |
Correct |
1 ms |
1628 KB |
Output is correct |
8 |
Correct |
1 ms |
1628 KB |
Output is correct |
9 |
Correct |
1 ms |
1760 KB |
Output is correct |
10 |
Correct |
1 ms |
1884 KB |
Output is correct |
11 |
Correct |
1 ms |
1884 KB |
Output is correct |
12 |
Correct |
1 ms |
1628 KB |
Output is correct |
13 |
Correct |
1 ms |
1628 KB |
Output is correct |
14 |
Correct |
1 ms |
1884 KB |
Output is correct |
15 |
Correct |
1 ms |
1884 KB |
Output is correct |
16 |
Correct |
1 ms |
1880 KB |
Output is correct |
17 |
Correct |
2 ms |
1884 KB |
Output is correct |
18 |
Correct |
1 ms |
1884 KB |
Output is correct |
19 |
Correct |
1 ms |
1884 KB |
Output is correct |
20 |
Correct |
2 ms |
1884 KB |
Output is correct |
21 |
Correct |
2 ms |
1884 KB |
Output is correct |
22 |
Correct |
1 ms |
1884 KB |
Output is correct |
23 |
Correct |
2 ms |
1880 KB |
Output is correct |
24 |
Correct |
2 ms |
1884 KB |
Output is correct |
25 |
Correct |
2 ms |
1884 KB |
Output is correct |
26 |
Correct |
1 ms |
1884 KB |
Output is correct |
27 |
Correct |
1 ms |
1884 KB |
Output is correct |
28 |
Correct |
2 ms |
1884 KB |
Output is correct |
29 |
Correct |
4 ms |
1884 KB |
Output is correct |
30 |
Correct |
2 ms |
1884 KB |
Output is correct |
31 |
Correct |
2 ms |
1884 KB |
Output is correct |
32 |
Correct |
2 ms |
1884 KB |
Output is correct |
33 |
Correct |
6 ms |
1884 KB |
Output is correct |
34 |
Correct |
6 ms |
2060 KB |
Output is correct |
35 |
Correct |
11 ms |
2908 KB |
Output is correct |
36 |
Correct |
2 ms |
1988 KB |
Output is correct |
37 |
Correct |
12 ms |
2652 KB |
Output is correct |
38 |
Correct |
14 ms |
3140 KB |
Output is correct |
39 |
Correct |
14 ms |
3160 KB |
Output is correct |
40 |
Correct |
16 ms |
3164 KB |
Output is correct |
41 |
Correct |
14 ms |
3260 KB |
Output is correct |
42 |
Correct |
4 ms |
1884 KB |
Output is correct |
43 |
Correct |
4 ms |
1880 KB |
Output is correct |
44 |
Correct |
5 ms |
1884 KB |
Output is correct |
45 |
Correct |
14 ms |
3160 KB |
Output is correct |
46 |
Correct |
14 ms |
3164 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
1628 KB |
Output is correct |
2 |
Correct |
1 ms |
1880 KB |
Output is correct |
3 |
Correct |
1 ms |
1868 KB |
Output is correct |
4 |
Correct |
1 ms |
1628 KB |
Output is correct |
5 |
Correct |
1 ms |
1628 KB |
Output is correct |
6 |
Correct |
1 ms |
1628 KB |
Output is correct |
7 |
Correct |
2 ms |
1628 KB |
Output is correct |
8 |
Correct |
1 ms |
1628 KB |
Output is correct |
9 |
Correct |
1 ms |
1628 KB |
Output is correct |
10 |
Correct |
1 ms |
1884 KB |
Output is correct |
11 |
Correct |
1 ms |
1884 KB |
Output is correct |
12 |
Correct |
1 ms |
1628 KB |
Output is correct |
13 |
Correct |
1 ms |
1624 KB |
Output is correct |
14 |
Correct |
1 ms |
1884 KB |
Output is correct |
15 |
Correct |
1 ms |
1884 KB |
Output is correct |
16 |
Correct |
1 ms |
1880 KB |
Output is correct |
17 |
Correct |
2 ms |
1880 KB |
Output is correct |
18 |
Correct |
2 ms |
1880 KB |
Output is correct |
19 |
Correct |
1 ms |
1884 KB |
Output is correct |
20 |
Correct |
1 ms |
1884 KB |
Output is correct |
21 |
Correct |
2 ms |
1884 KB |
Output is correct |
22 |
Correct |
1 ms |
1884 KB |
Output is correct |
23 |
Correct |
2 ms |
1884 KB |
Output is correct |
24 |
Correct |
2 ms |
2136 KB |
Output is correct |
25 |
Correct |
2 ms |
1884 KB |
Output is correct |
26 |
Correct |
2 ms |
1884 KB |
Output is correct |
27 |
Correct |
2 ms |
1884 KB |
Output is correct |
28 |
Correct |
1 ms |
1884 KB |
Output is correct |
29 |
Correct |
5 ms |
1884 KB |
Output is correct |
30 |
Correct |
2 ms |
1884 KB |
Output is correct |
31 |
Correct |
2 ms |
1884 KB |
Output is correct |
32 |
Correct |
2 ms |
1884 KB |
Output is correct |
33 |
Correct |
6 ms |
1884 KB |
Output is correct |
34 |
Correct |
7 ms |
2064 KB |
Output is correct |
35 |
Correct |
11 ms |
2904 KB |
Output is correct |
36 |
Correct |
3 ms |
1884 KB |
Output is correct |
37 |
Correct |
12 ms |
2860 KB |
Output is correct |
38 |
Correct |
16 ms |
3136 KB |
Output is correct |
39 |
Correct |
19 ms |
3364 KB |
Output is correct |
40 |
Correct |
18 ms |
3164 KB |
Output is correct |
41 |
Correct |
15 ms |
3128 KB |
Output is correct |
42 |
Correct |
4 ms |
1884 KB |
Output is correct |
43 |
Correct |
4 ms |
1884 KB |
Output is correct |
44 |
Correct |
4 ms |
1884 KB |
Output is correct |
45 |
Correct |
15 ms |
3276 KB |
Output is correct |
46 |
Correct |
16 ms |
3164 KB |
Output is correct |
47 |
Correct |
34 ms |
3576 KB |
Output is correct |
48 |
Correct |
10 ms |
3088 KB |
Output is correct |
49 |
Correct |
5 ms |
2904 KB |
Output is correct |
50 |
Correct |
4 ms |
2652 KB |
Output is correct |
51 |
Correct |
52 ms |
4716 KB |
Output is correct |
52 |
Correct |
40 ms |
4440 KB |
Output is correct |
53 |
Correct |
21 ms |
4444 KB |
Output is correct |
54 |
Correct |
4 ms |
2652 KB |
Output is correct |
55 |
Correct |
6 ms |
2652 KB |
Output is correct |
56 |
Correct |
10 ms |
3420 KB |
Output is correct |
57 |
Correct |
42 ms |
2976 KB |
Output is correct |
58 |
Correct |
8 ms |
2808 KB |
Output is correct |
59 |
Correct |
11 ms |
2908 KB |
Output is correct |
60 |
Correct |
12 ms |
2912 KB |
Output is correct |
61 |
Correct |
11 ms |
3164 KB |
Output is correct |
62 |
Correct |
10 ms |
3688 KB |
Output is correct |
63 |
Correct |
72 ms |
5016 KB |
Output is correct |
64 |
Correct |
66 ms |
4920 KB |
Output is correct |
65 |
Correct |
89 ms |
4992 KB |
Output is correct |
66 |
Correct |
156 ms |
4956 KB |
Output is correct |
67 |
Correct |
168 ms |
5008 KB |
Output is correct |