#include <bits/stdc++.h>
using namespace std;
#define f first
#define s second
#define int long long
const int N = 30069;
const int inf = 1e18;
int n, m, a[N], b[N], dis[N];;
vector<int> c[N];
vector<pair<int, int>> g[N];
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int tt = 1;
//cin >> tt;
while (tt--) {
cin >> n >> m;
for (int i = 1;i <= m;i++) {
cin >> a[i] >> b[i], ++a[i];
c[a[i]].push_back(b[i]);
}
for (int i = 1;i <= n;i++) {
dis[i] = inf;
}
priority_queue<pair<int, int>> q;
q.push({0, a[1]});
dis[a[1]] = 0;
while (!q.empty()) {
auto v = q.top().s;
q.pop();
if (v == a[2])
break;
for (auto p : c[v]) {
for (int j = v, d = 0;j <= n;j += p,d++) {
if (dis[j] <= dis[v] + d)
continue;
dis[j] = dis[v] + d;
q.push({-dis[j], j});
}
for (int j = v, d = 0;j >= 1;j -= p,d++) {
if (dis[j] <= dis[v] + d)
continue;
dis[j] = dis[v] + d;
q.push({-dis[j], j});
}
}
}
if (dis[a[2]] == inf)
cout << -1 << '\n';
else
cout << dis[a[2]] << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1624 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 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1884 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 |
1884 KB |
Output is correct |
8 |
Correct |
1 ms |
1628 KB |
Output is correct |
9 |
Correct |
1 ms |
1884 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 |
1880 KB |
Output is correct |
13 |
Correct |
1 ms |
1884 KB |
Output is correct |
14 |
Correct |
1 ms |
1884 KB |
Output is correct |
15 |
Correct |
1 ms |
1884 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
1880 KB |
Output is correct |
6 |
Correct |
1 ms |
1868 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 |
1884 KB |
Output is correct |
10 |
Correct |
1 ms |
1884 KB |
Output is correct |
11 |
Correct |
2 ms |
2136 KB |
Output is correct |
12 |
Correct |
1 ms |
1884 KB |
Output is correct |
13 |
Correct |
1 ms |
1884 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 |
1 ms |
2392 KB |
Output is correct |
18 |
Correct |
1 ms |
1884 KB |
Output is correct |
19 |
Correct |
1 ms |
1884 KB |
Output is correct |
20 |
Correct |
4 ms |
1884 KB |
Output is correct |
21 |
Correct |
1 ms |
1728 KB |
Output is correct |
22 |
Correct |
1 ms |
1884 KB |
Output is correct |
23 |
Correct |
1 ms |
1884 KB |
Output is correct |
24 |
Correct |
1 ms |
1884 KB |
Output is correct |
25 |
Correct |
1 ms |
1928 KB |
Output is correct |
26 |
Correct |
1 ms |
1884 KB |
Output is correct |
27 |
Correct |
1 ms |
1884 KB |
Output is correct |
28 |
Correct |
1 ms |
1884 KB |
Output is correct |
29 |
Correct |
2 ms |
2140 KB |
Output is correct |
30 |
Correct |
1 ms |
1884 KB |
Output is correct |
31 |
Correct |
1 ms |
1884 KB |
Output is correct |
32 |
Correct |
1 ms |
1884 KB |
Output is correct |
33 |
Correct |
2 ms |
2136 KB |
Output is correct |
34 |
Correct |
2 ms |
2136 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1624 KB |
Output is correct |
2 |
Correct |
1 ms |
1884 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 |
1884 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 |
1884 KB |
Output is correct |
9 |
Correct |
1 ms |
1884 KB |
Output is correct |
10 |
Correct |
1 ms |
2136 KB |
Output is correct |
11 |
Correct |
1 ms |
1884 KB |
Output is correct |
12 |
Correct |
1 ms |
1884 KB |
Output is correct |
13 |
Correct |
2 ms |
1884 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 |
1 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 |
4 ms |
1884 KB |
Output is correct |
21 |
Correct |
1 ms |
1884 KB |
Output is correct |
22 |
Correct |
1 ms |
1912 KB |
Output is correct |
23 |
Correct |
1 ms |
1884 KB |
Output is correct |
24 |
Correct |
1 ms |
1884 KB |
Output is correct |
25 |
Correct |
1 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 |
1 ms |
1884 KB |
Output is correct |
29 |
Correct |
2 ms |
2140 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 |
2 ms |
2140 KB |
Output is correct |
34 |
Correct |
2 ms |
2140 KB |
Output is correct |
35 |
Correct |
5 ms |
2648 KB |
Output is correct |
36 |
Correct |
2 ms |
1884 KB |
Output is correct |
37 |
Correct |
4 ms |
2648 KB |
Output is correct |
38 |
Correct |
6 ms |
2908 KB |
Output is correct |
39 |
Correct |
5 ms |
2652 KB |
Output is correct |
40 |
Correct |
5 ms |
2960 KB |
Output is correct |
41 |
Correct |
6 ms |
2908 KB |
Output is correct |
42 |
Correct |
4 ms |
2524 KB |
Output is correct |
43 |
Correct |
4 ms |
2524 KB |
Output is correct |
44 |
Correct |
43 ms |
2684 KB |
Output is correct |
45 |
Correct |
6 ms |
2904 KB |
Output is correct |
46 |
Correct |
5 ms |
2904 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1628 KB |
Output is correct |
2 |
Correct |
1 ms |
1628 KB |
Output is correct |
3 |
Correct |
1 ms |
1884 KB |
Output is correct |
4 |
Correct |
1 ms |
1628 KB |
Output is correct |
5 |
Correct |
1 ms |
1884 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 |
1884 KB |
Output is correct |
9 |
Correct |
1 ms |
1628 KB |
Output is correct |
10 |
Correct |
2 ms |
1880 KB |
Output is correct |
11 |
Correct |
1 ms |
1884 KB |
Output is correct |
12 |
Correct |
1 ms |
1884 KB |
Output is correct |
13 |
Correct |
1 ms |
1884 KB |
Output is correct |
14 |
Correct |
2 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 |
1 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 |
4 ms |
1880 KB |
Output is correct |
21 |
Correct |
1 ms |
1884 KB |
Output is correct |
22 |
Correct |
1 ms |
1884 KB |
Output is correct |
23 |
Correct |
1 ms |
1880 KB |
Output is correct |
24 |
Correct |
2 ms |
1884 KB |
Output is correct |
25 |
Correct |
1 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 |
1 ms |
1884 KB |
Output is correct |
29 |
Correct |
2 ms |
2140 KB |
Output is correct |
30 |
Correct |
1 ms |
1884 KB |
Output is correct |
31 |
Correct |
1 ms |
1884 KB |
Output is correct |
32 |
Correct |
1 ms |
1884 KB |
Output is correct |
33 |
Correct |
2 ms |
2140 KB |
Output is correct |
34 |
Correct |
2 ms |
2140 KB |
Output is correct |
35 |
Correct |
5 ms |
2536 KB |
Output is correct |
36 |
Correct |
2 ms |
1884 KB |
Output is correct |
37 |
Correct |
4 ms |
2652 KB |
Output is correct |
38 |
Correct |
6 ms |
2828 KB |
Output is correct |
39 |
Correct |
5 ms |
2652 KB |
Output is correct |
40 |
Correct |
5 ms |
2908 KB |
Output is correct |
41 |
Correct |
6 ms |
2908 KB |
Output is correct |
42 |
Correct |
4 ms |
2524 KB |
Output is correct |
43 |
Correct |
4 ms |
2524 KB |
Output is correct |
44 |
Correct |
50 ms |
2564 KB |
Output is correct |
45 |
Correct |
7 ms |
2904 KB |
Output is correct |
46 |
Correct |
6 ms |
2908 KB |
Output is correct |
47 |
Correct |
7 ms |
3804 KB |
Output is correct |
48 |
Correct |
5 ms |
2652 KB |
Output is correct |
49 |
Correct |
5 ms |
2652 KB |
Output is correct |
50 |
Correct |
4 ms |
2652 KB |
Output is correct |
51 |
Correct |
12 ms |
5332 KB |
Output is correct |
52 |
Correct |
13 ms |
4216 KB |
Output is correct |
53 |
Correct |
7 ms |
3544 KB |
Output is correct |
54 |
Correct |
2 ms |
2396 KB |
Output is correct |
55 |
Correct |
2 ms |
2396 KB |
Output is correct |
56 |
Correct |
626 ms |
4060 KB |
Output is correct |
57 |
Correct |
4 ms |
4300 KB |
Output is correct |
58 |
Correct |
584 ms |
3160 KB |
Output is correct |
59 |
Correct |
6 ms |
3416 KB |
Output is correct |
60 |
Correct |
6 ms |
3164 KB |
Output is correct |
61 |
Correct |
6 ms |
3184 KB |
Output is correct |
62 |
Correct |
7 ms |
3672 KB |
Output is correct |
63 |
Correct |
20 ms |
5140 KB |
Output is correct |
64 |
Correct |
19 ms |
5092 KB |
Output is correct |
65 |
Correct |
23 ms |
5072 KB |
Output is correct |
66 |
Correct |
38 ms |
7376 KB |
Output is correct |
67 |
Correct |
31 ms |
8912 KB |
Output is correct |