#include <bits/stdc++.h>
using namespace std;
using i64 = int64_t;
constexpr int inf = 1 << 30;
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REP3(i, l, r) for (int i = (l); i < (r); ++i)
#define RVP(i, n) for (int i = (n - 1); i >= 0; --i)
#define ALL(x) (x).begin(), (x).end()
template <typename T> bool chmin(T &a, const T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
vector<int> calc_mincost(int S, vector<vector<pair<int, int>>> &g) {
const int N = (int)g.size();
vector<int> dist(N, inf);
dist[S] = 0;
deque<int> deq;
deq.push_back(S);
while (not deq.empty()) {
const int f = deq.front();
deq.pop_front();
for (const auto &[t, c] : g[f]) {
if (chmin(dist[t], dist[f] + c)) {
if (c == 0) {
deq.push_front(t);
} else {
deq.push_back(t);
}
}
}
}
return dist;
}
int main() {
int N, M;
cin >> N >> M;
vector<pair<int, int>> A(M);
REP(i, M) {
cin >> A[i].first >> A[i].second;
chmin(A[i].second, N);
}
int S = 0, T = 0;
{
const auto as = A[0], at = A[1];
sort(ALL(A), [](auto a, auto b) {
if (a.second != b.second) {
return a.second < b.second;
}
return a.first < b.first;
});
REP(i, M) {
if (A[i] == as) S = i;
if (A[i] == at) T = i;
}
}
vector<vector<pair<int, bool>>> lst(N);
REP(i, M) {
if ((not lst[A[i].first].empty()) and lst[A[i].first].back().first == A[i].second) {
lst[A[i].first].back().second = true;
continue;
} else {
lst[A[i].first].push_back({A[i].second, true});
}
int f = A[i].first % A[i].second;
while (f < N) {
lst[f].push_back({A[i].second, false});
f += A[i].second;
if (f == A[i].first) f += A[i].second;
}
}
vector<int> size_r(N + 1);
REP(i, N) size_r[i + 1] = size_r[i] + (int)lst[i].size();
vector<unordered_map<int, int>> id_m(N);
REP(i, N) { REP(j, (int)lst[i].size()) id_m[i][lst[i][j].first] = size_r[i] + j; }
const int V = size_r[N] + N;
assert(V <= 1500000);
vector<vector<pair<int, int>>> graph(V);
REP(i, N) {
for (const auto &[w, c] : lst[i]) {
const int id = id_m[i][w];
graph[id].push_back({size_r[N] + i, 0});
if (c) graph[size_r[N] + i].push_back({id, 0});
const int l = i - w;
if (l >= 0) {
const int id2 = id_m[l][w];
graph[id2].push_back({id, 1});
graph[id].push_back({id2, 1});
}
}
}
auto dist = calc_mincost(size_r[N] + A[S].first, graph);
auto ans = dist[size_r[N] + A[T].first];
if (ans == inf) ans = -1;
cout << ans << endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
3 ms |
816 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
3 ms |
1108 KB |
Output is correct |
15 |
Correct |
3 ms |
1108 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
3 ms |
852 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
3 ms |
1108 KB |
Output is correct |
15 |
Correct |
3 ms |
1108 KB |
Output is correct |
16 |
Correct |
2 ms |
724 KB |
Output is correct |
17 |
Correct |
6 ms |
2260 KB |
Output is correct |
18 |
Correct |
4 ms |
1492 KB |
Output is correct |
19 |
Correct |
4 ms |
1236 KB |
Output is correct |
20 |
Correct |
3 ms |
980 KB |
Output is correct |
21 |
Correct |
2 ms |
596 KB |
Output is correct |
22 |
Correct |
3 ms |
1364 KB |
Output is correct |
23 |
Correct |
3 ms |
1364 KB |
Output is correct |
24 |
Correct |
7 ms |
2260 KB |
Output is correct |
25 |
Correct |
5 ms |
1620 KB |
Output is correct |
26 |
Correct |
4 ms |
1620 KB |
Output is correct |
27 |
Correct |
4 ms |
1492 KB |
Output is correct |
28 |
Correct |
10 ms |
3592 KB |
Output is correct |
29 |
Correct |
26 ms |
8404 KB |
Output is correct |
30 |
Correct |
7 ms |
2772 KB |
Output is correct |
31 |
Correct |
12 ms |
4948 KB |
Output is correct |
32 |
Correct |
8 ms |
3540 KB |
Output is correct |
33 |
Correct |
50 ms |
15976 KB |
Output is correct |
34 |
Correct |
50 ms |
15996 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
260 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
304 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
2 ms |
852 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
3 ms |
1108 KB |
Output is correct |
15 |
Correct |
3 ms |
1108 KB |
Output is correct |
16 |
Correct |
2 ms |
724 KB |
Output is correct |
17 |
Correct |
6 ms |
2260 KB |
Output is correct |
18 |
Correct |
4 ms |
1492 KB |
Output is correct |
19 |
Correct |
3 ms |
1236 KB |
Output is correct |
20 |
Correct |
3 ms |
980 KB |
Output is correct |
21 |
Correct |
2 ms |
596 KB |
Output is correct |
22 |
Correct |
3 ms |
1364 KB |
Output is correct |
23 |
Correct |
4 ms |
1464 KB |
Output is correct |
24 |
Correct |
7 ms |
2260 KB |
Output is correct |
25 |
Correct |
5 ms |
1620 KB |
Output is correct |
26 |
Correct |
5 ms |
1588 KB |
Output is correct |
27 |
Correct |
4 ms |
1492 KB |
Output is correct |
28 |
Correct |
10 ms |
3540 KB |
Output is correct |
29 |
Correct |
22 ms |
8336 KB |
Output is correct |
30 |
Correct |
7 ms |
2900 KB |
Output is correct |
31 |
Correct |
12 ms |
4924 KB |
Output is correct |
32 |
Correct |
8 ms |
3540 KB |
Output is correct |
33 |
Correct |
50 ms |
15928 KB |
Output is correct |
34 |
Correct |
51 ms |
15956 KB |
Output is correct |
35 |
Correct |
59 ms |
13688 KB |
Output is correct |
36 |
Correct |
9 ms |
2644 KB |
Output is correct |
37 |
Correct |
103 ms |
22756 KB |
Output is correct |
38 |
Correct |
100 ms |
21960 KB |
Output is correct |
39 |
Correct |
105 ms |
21964 KB |
Output is correct |
40 |
Correct |
103 ms |
21972 KB |
Output is correct |
41 |
Correct |
101 ms |
21840 KB |
Output is correct |
42 |
Correct |
15 ms |
1876 KB |
Output is correct |
43 |
Correct |
16 ms |
1696 KB |
Output is correct |
44 |
Correct |
13 ms |
1312 KB |
Output is correct |
45 |
Correct |
199 ms |
60828 KB |
Output is correct |
46 |
Correct |
202 ms |
60796 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
2 ms |
852 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
3 ms |
1108 KB |
Output is correct |
15 |
Correct |
3 ms |
1108 KB |
Output is correct |
16 |
Correct |
2 ms |
724 KB |
Output is correct |
17 |
Correct |
6 ms |
2276 KB |
Output is correct |
18 |
Correct |
4 ms |
1492 KB |
Output is correct |
19 |
Correct |
3 ms |
1236 KB |
Output is correct |
20 |
Correct |
3 ms |
980 KB |
Output is correct |
21 |
Correct |
2 ms |
596 KB |
Output is correct |
22 |
Correct |
3 ms |
1364 KB |
Output is correct |
23 |
Correct |
4 ms |
1364 KB |
Output is correct |
24 |
Correct |
6 ms |
2260 KB |
Output is correct |
25 |
Correct |
5 ms |
1620 KB |
Output is correct |
26 |
Correct |
4 ms |
1620 KB |
Output is correct |
27 |
Correct |
4 ms |
1492 KB |
Output is correct |
28 |
Correct |
11 ms |
3592 KB |
Output is correct |
29 |
Correct |
23 ms |
8316 KB |
Output is correct |
30 |
Correct |
6 ms |
2840 KB |
Output is correct |
31 |
Correct |
11 ms |
4892 KB |
Output is correct |
32 |
Correct |
8 ms |
3484 KB |
Output is correct |
33 |
Correct |
50 ms |
15956 KB |
Output is correct |
34 |
Correct |
51 ms |
16000 KB |
Output is correct |
35 |
Correct |
58 ms |
13688 KB |
Output is correct |
36 |
Correct |
9 ms |
2644 KB |
Output is correct |
37 |
Correct |
97 ms |
22732 KB |
Output is correct |
38 |
Correct |
98 ms |
21904 KB |
Output is correct |
39 |
Correct |
102 ms |
21984 KB |
Output is correct |
40 |
Correct |
100 ms |
21900 KB |
Output is correct |
41 |
Correct |
100 ms |
21852 KB |
Output is correct |
42 |
Correct |
15 ms |
1828 KB |
Output is correct |
43 |
Correct |
15 ms |
1780 KB |
Output is correct |
44 |
Correct |
14 ms |
1236 KB |
Output is correct |
45 |
Correct |
209 ms |
60824 KB |
Output is correct |
46 |
Correct |
199 ms |
60908 KB |
Output is correct |
47 |
Correct |
514 ms |
96716 KB |
Output is correct |
48 |
Correct |
145 ms |
34012 KB |
Output is correct |
49 |
Correct |
108 ms |
23652 KB |
Output is correct |
50 |
Correct |
86 ms |
24012 KB |
Output is correct |
51 |
Correct |
242 ms |
45776 KB |
Output is correct |
52 |
Correct |
293 ms |
49440 KB |
Output is correct |
53 |
Correct |
70 ms |
19224 KB |
Output is correct |
54 |
Correct |
22 ms |
10964 KB |
Output is correct |
55 |
Correct |
30 ms |
13608 KB |
Output is correct |
56 |
Correct |
36 ms |
12108 KB |
Output is correct |
57 |
Correct |
229 ms |
60492 KB |
Output is correct |
58 |
Correct |
43 ms |
15308 KB |
Output is correct |
59 |
Correct |
60 ms |
18684 KB |
Output is correct |
60 |
Correct |
75 ms |
22044 KB |
Output is correct |
61 |
Correct |
72 ms |
20256 KB |
Output is correct |
62 |
Correct |
343 ms |
69280 KB |
Output is correct |
63 |
Runtime error |
379 ms |
262144 KB |
Execution killed with signal 6 |
64 |
Halted |
0 ms |
0 KB |
- |