#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// #define int ll
#define pb push_back
typedef pair<int, int> pii;
#define all(x) (x).begin(), (x).end()
#define F first
#define S second
const int INF = 1e9;
bool cmin(int &a, const int &b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
void run() {
int n, m;
cin >> n >> m;
vector<int> b(m), p(m);
vector<vector<int>> who(n);
for (int i = 0; i < m; ++i) {
cin >> b[i] >> p[i];
who[b[i]].pb(p[i]);
}
vector<vector<pii>> g(n);
vector<int> dist(n, INF);
priority_queue<pii> q;
dist[b[0]] = 0;
q.push({0, b[0]});
while (!q.empty()) {
pii pr = q.top();
q.pop();
if (-pr.F != dist[pr.S])
continue;
int u = pr.S;
sort(all(who[u]));
who[u].resize(unique(all(who[u])) - who[u].begin());
for (auto x : who[u]) {
int cnt = 1;
for (int i = u + x; i < n; i += x) {
// g[u].pb({i, cnt});
if (cmin(dist[i], dist[u] + cnt)) {
q.push({-dist[i], i});
}
++cnt;
}
cnt = 1;
for (int i = u - x; i >= 0; i -= x) {
// g[u].pb({i, cnt});
if (cmin(dist[i], dist[u] + cnt)) {
q.push({-dist[i], i});
}
++cnt;
}
}
// for (auto edge : g[u]) {
// int v = edge.F;
// int w = edge.S;
// if (cmin(dist[v], dist[u] + w)) {
// q.push({-dist[v], v});
// }
// }
}
if (dist[b[1]] >= INF)
dist[b[1]] = -1;
cout << dist[b[1]] << '\n';
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
run();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
3 ms |
256 KB |
Output is correct |
3 |
Correct |
2 ms |
256 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
3 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
3 ms |
384 KB |
Output is correct |
12 |
Correct |
2 ms |
384 KB |
Output is correct |
13 |
Correct |
2 ms |
384 KB |
Output is correct |
14 |
Correct |
4 ms |
384 KB |
Output is correct |
15 |
Correct |
2 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
3 ms |
384 KB |
Output is correct |
9 |
Correct |
4 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
3 ms |
384 KB |
Output is correct |
12 |
Correct |
3 ms |
512 KB |
Output is correct |
13 |
Correct |
3 ms |
384 KB |
Output is correct |
14 |
Correct |
3 ms |
384 KB |
Output is correct |
15 |
Correct |
3 ms |
384 KB |
Output is correct |
16 |
Correct |
2 ms |
384 KB |
Output is correct |
17 |
Correct |
3 ms |
512 KB |
Output is correct |
18 |
Correct |
2 ms |
512 KB |
Output is correct |
19 |
Correct |
2 ms |
384 KB |
Output is correct |
20 |
Correct |
5 ms |
512 KB |
Output is correct |
21 |
Correct |
3 ms |
384 KB |
Output is correct |
22 |
Correct |
3 ms |
384 KB |
Output is correct |
23 |
Correct |
3 ms |
384 KB |
Output is correct |
24 |
Correct |
4 ms |
512 KB |
Output is correct |
25 |
Correct |
3 ms |
512 KB |
Output is correct |
26 |
Correct |
3 ms |
512 KB |
Output is correct |
27 |
Correct |
4 ms |
512 KB |
Output is correct |
28 |
Correct |
3 ms |
512 KB |
Output is correct |
29 |
Correct |
4 ms |
640 KB |
Output is correct |
30 |
Correct |
3 ms |
640 KB |
Output is correct |
31 |
Correct |
4 ms |
512 KB |
Output is correct |
32 |
Correct |
3 ms |
512 KB |
Output is correct |
33 |
Correct |
4 ms |
768 KB |
Output is correct |
34 |
Correct |
6 ms |
640 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
384 KB |
Output is correct |
2 |
Correct |
3 ms |
384 KB |
Output is correct |
3 |
Correct |
3 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
3 ms |
384 KB |
Output is correct |
10 |
Correct |
3 ms |
384 KB |
Output is correct |
11 |
Correct |
3 ms |
384 KB |
Output is correct |
12 |
Correct |
3 ms |
384 KB |
Output is correct |
13 |
Correct |
3 ms |
384 KB |
Output is correct |
14 |
Correct |
3 ms |
384 KB |
Output is correct |
15 |
Correct |
4 ms |
384 KB |
Output is correct |
16 |
Correct |
3 ms |
384 KB |
Output is correct |
17 |
Correct |
3 ms |
484 KB |
Output is correct |
18 |
Correct |
2 ms |
384 KB |
Output is correct |
19 |
Correct |
2 ms |
512 KB |
Output is correct |
20 |
Correct |
5 ms |
640 KB |
Output is correct |
21 |
Correct |
2 ms |
384 KB |
Output is correct |
22 |
Correct |
2 ms |
384 KB |
Output is correct |
23 |
Correct |
3 ms |
512 KB |
Output is correct |
24 |
Correct |
4 ms |
512 KB |
Output is correct |
25 |
Correct |
3 ms |
512 KB |
Output is correct |
26 |
Correct |
2 ms |
640 KB |
Output is correct |
27 |
Correct |
2 ms |
512 KB |
Output is correct |
28 |
Correct |
2 ms |
512 KB |
Output is correct |
29 |
Correct |
3 ms |
640 KB |
Output is correct |
30 |
Correct |
3 ms |
512 KB |
Output is correct |
31 |
Correct |
3 ms |
640 KB |
Output is correct |
32 |
Correct |
3 ms |
512 KB |
Output is correct |
33 |
Correct |
5 ms |
768 KB |
Output is correct |
34 |
Correct |
4 ms |
768 KB |
Output is correct |
35 |
Correct |
10 ms |
1024 KB |
Output is correct |
36 |
Correct |
5 ms |
484 KB |
Output is correct |
37 |
Correct |
9 ms |
1024 KB |
Output is correct |
38 |
Correct |
12 ms |
1280 KB |
Output is correct |
39 |
Correct |
14 ms |
1280 KB |
Output is correct |
40 |
Correct |
11 ms |
1280 KB |
Output is correct |
41 |
Correct |
16 ms |
1432 KB |
Output is correct |
42 |
Correct |
8 ms |
1024 KB |
Output is correct |
43 |
Correct |
9 ms |
1024 KB |
Output is correct |
44 |
Correct |
11 ms |
1024 KB |
Output is correct |
45 |
Correct |
13 ms |
1280 KB |
Output is correct |
46 |
Correct |
13 ms |
1280 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
256 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
3 ms |
384 KB |
Output is correct |
11 |
Correct |
3 ms |
384 KB |
Output is correct |
12 |
Correct |
3 ms |
384 KB |
Output is correct |
13 |
Correct |
3 ms |
384 KB |
Output is correct |
14 |
Correct |
4 ms |
384 KB |
Output is correct |
15 |
Correct |
4 ms |
384 KB |
Output is correct |
16 |
Correct |
2 ms |
384 KB |
Output is correct |
17 |
Correct |
3 ms |
384 KB |
Output is correct |
18 |
Correct |
2 ms |
384 KB |
Output is correct |
19 |
Correct |
2 ms |
512 KB |
Output is correct |
20 |
Correct |
6 ms |
512 KB |
Output is correct |
21 |
Correct |
2 ms |
384 KB |
Output is correct |
22 |
Correct |
2 ms |
384 KB |
Output is correct |
23 |
Correct |
3 ms |
512 KB |
Output is correct |
24 |
Correct |
3 ms |
512 KB |
Output is correct |
25 |
Correct |
3 ms |
512 KB |
Output is correct |
26 |
Correct |
3 ms |
512 KB |
Output is correct |
27 |
Correct |
3 ms |
512 KB |
Output is correct |
28 |
Correct |
0 ms |
512 KB |
Output is correct |
29 |
Correct |
4 ms |
640 KB |
Output is correct |
30 |
Correct |
3 ms |
512 KB |
Output is correct |
31 |
Correct |
3 ms |
640 KB |
Output is correct |
32 |
Correct |
3 ms |
512 KB |
Output is correct |
33 |
Correct |
4 ms |
640 KB |
Output is correct |
34 |
Correct |
4 ms |
768 KB |
Output is correct |
35 |
Correct |
10 ms |
1152 KB |
Output is correct |
36 |
Correct |
4 ms |
512 KB |
Output is correct |
37 |
Correct |
9 ms |
1024 KB |
Output is correct |
38 |
Correct |
11 ms |
1280 KB |
Output is correct |
39 |
Correct |
14 ms |
1152 KB |
Output is correct |
40 |
Correct |
12 ms |
1280 KB |
Output is correct |
41 |
Correct |
13 ms |
1280 KB |
Output is correct |
42 |
Correct |
9 ms |
1096 KB |
Output is correct |
43 |
Correct |
9 ms |
1024 KB |
Output is correct |
44 |
Correct |
11 ms |
1024 KB |
Output is correct |
45 |
Correct |
15 ms |
1408 KB |
Output is correct |
46 |
Correct |
13 ms |
1328 KB |
Output is correct |
47 |
Correct |
20 ms |
2428 KB |
Output is correct |
48 |
Correct |
14 ms |
2404 KB |
Output is correct |
49 |
Correct |
10 ms |
2432 KB |
Output is correct |
50 |
Correct |
9 ms |
2432 KB |
Output is correct |
51 |
Correct |
52 ms |
4208 KB |
Output is correct |
52 |
Correct |
49 ms |
3700 KB |
Output is correct |
53 |
Correct |
22 ms |
3708 KB |
Output is correct |
54 |
Correct |
6 ms |
2048 KB |
Output is correct |
55 |
Correct |
6 ms |
2176 KB |
Output is correct |
56 |
Correct |
715 ms |
3712 KB |
Output is correct |
57 |
Correct |
51 ms |
4084 KB |
Output is correct |
58 |
Correct |
10 ms |
2560 KB |
Output is correct |
59 |
Correct |
12 ms |
2816 KB |
Output is correct |
60 |
Correct |
12 ms |
2688 KB |
Output is correct |
61 |
Correct |
13 ms |
2688 KB |
Output is correct |
62 |
Correct |
24 ms |
3328 KB |
Output is correct |
63 |
Correct |
36 ms |
3704 KB |
Output is correct |
64 |
Correct |
36 ms |
3704 KB |
Output is correct |
65 |
Correct |
35 ms |
3704 KB |
Output is correct |
66 |
Correct |
67 ms |
4588 KB |
Output is correct |
67 |
Correct |
64 ms |
4596 KB |
Output is correct |