#include <bits/stdc++.h>
#define ll long long
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define pb push_back
#define FIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
using namespace std;
typedef pair <int, int> pii;
const int N = 30007, B = 200, inf = 1e9;
int n, m;
int b[N], p[N], dis[N*B];
vector <int> td[N], gt[N];
set <pii> s;
int En(int x, int y = 0) {return y*N + x;}
pii De(int x) {return {x%N, x/N};}
void Vis(int x, int y, int w) {
if (dis[x] + w < dis[y]) {
if (dis[y] != inf) s.erase({dis[y], y});
dis[y] = dis[x] + w;
s.insert({dis[y], y});
}
}
int main () {
FIO;
cin >> n >> m;
for (int i = 0; i < m; i++) {
cin >> b[i] >> p[i];
if (p[i] < B) gt[b[i]].pb(p[i]);
else td[b[i]].pb(p[i]);
}
for (int i = 0; i < N*B; i++) dis[i] = inf;
s.insert({0, b[0]});
dis[b[0]] = 0;
while (!s.empty()) {
auto p = s.begin();
int x = (*p).S;
s.erase(p);
pii de = De(x);
int A = de.F, C = de.S;
if (C > 0) {
if (A-C >= 0) Vis(x, En(A-C, C), 1);
if (A+C < n) Vis(x, En(A+C, C), 1);
Vis(x, A, 0);
}
else {
for (auto P : gt[x]) Vis(x, En(x, P), 0);
for (auto P : td[x]) {
for (int j = A-P, k = 1; j >= 0; j -= P, k++) Vis(x, j, k);
for (int j = A+P, k = 1; j < n; j += P, k++) Vis(x, j, k);
}
}
}
if (dis[b[1]] != inf) cout << dis[b[1]] << "\n";
else cout << "-1\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
25180 KB |
Output is correct |
2 |
Correct |
8 ms |
25180 KB |
Output is correct |
3 |
Correct |
5 ms |
25176 KB |
Output is correct |
4 |
Correct |
8 ms |
25180 KB |
Output is correct |
5 |
Correct |
5 ms |
25180 KB |
Output is correct |
6 |
Correct |
7 ms |
25180 KB |
Output is correct |
7 |
Correct |
5 ms |
25180 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
25180 KB |
Output is correct |
2 |
Correct |
5 ms |
25184 KB |
Output is correct |
3 |
Correct |
5 ms |
25356 KB |
Output is correct |
4 |
Correct |
5 ms |
25180 KB |
Output is correct |
5 |
Correct |
5 ms |
25180 KB |
Output is correct |
6 |
Correct |
5 ms |
25240 KB |
Output is correct |
7 |
Correct |
6 ms |
25176 KB |
Output is correct |
8 |
Correct |
5 ms |
25176 KB |
Output is correct |
9 |
Correct |
5 ms |
25176 KB |
Output is correct |
10 |
Correct |
5 ms |
25180 KB |
Output is correct |
11 |
Correct |
6 ms |
25688 KB |
Output is correct |
12 |
Correct |
5 ms |
25180 KB |
Output is correct |
13 |
Correct |
5 ms |
25180 KB |
Output is correct |
14 |
Correct |
6 ms |
25436 KB |
Output is correct |
15 |
Correct |
6 ms |
25436 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
25264 KB |
Output is correct |
2 |
Correct |
5 ms |
25180 KB |
Output is correct |
3 |
Correct |
5 ms |
25176 KB |
Output is correct |
4 |
Correct |
5 ms |
25180 KB |
Output is correct |
5 |
Correct |
5 ms |
25180 KB |
Output is correct |
6 |
Correct |
5 ms |
25180 KB |
Output is correct |
7 |
Correct |
5 ms |
25180 KB |
Output is correct |
8 |
Correct |
5 ms |
25176 KB |
Output is correct |
9 |
Correct |
6 ms |
25180 KB |
Output is correct |
10 |
Correct |
5 ms |
25272 KB |
Output is correct |
11 |
Correct |
6 ms |
25436 KB |
Output is correct |
12 |
Correct |
5 ms |
25180 KB |
Output is correct |
13 |
Correct |
5 ms |
25176 KB |
Output is correct |
14 |
Correct |
6 ms |
25436 KB |
Output is correct |
15 |
Correct |
6 ms |
25268 KB |
Output is correct |
16 |
Correct |
6 ms |
25280 KB |
Output is correct |
17 |
Correct |
7 ms |
25300 KB |
Output is correct |
18 |
Correct |
5 ms |
25180 KB |
Output is correct |
19 |
Correct |
5 ms |
25180 KB |
Output is correct |
20 |
Correct |
6 ms |
25444 KB |
Output is correct |
21 |
Correct |
5 ms |
25176 KB |
Output is correct |
22 |
Correct |
5 ms |
25176 KB |
Output is correct |
23 |
Correct |
6 ms |
25180 KB |
Output is correct |
24 |
Correct |
6 ms |
25436 KB |
Output is correct |
25 |
Correct |
6 ms |
25180 KB |
Output is correct |
26 |
Correct |
6 ms |
25180 KB |
Output is correct |
27 |
Correct |
5 ms |
25180 KB |
Output is correct |
28 |
Correct |
6 ms |
25328 KB |
Output is correct |
29 |
Correct |
10 ms |
25272 KB |
Output is correct |
30 |
Correct |
7 ms |
25180 KB |
Output is correct |
31 |
Correct |
8 ms |
25264 KB |
Output is correct |
32 |
Correct |
7 ms |
25180 KB |
Output is correct |
33 |
Correct |
16 ms |
25484 KB |
Output is correct |
34 |
Correct |
15 ms |
25436 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
25176 KB |
Output is correct |
2 |
Correct |
5 ms |
25180 KB |
Output is correct |
3 |
Correct |
5 ms |
25224 KB |
Output is correct |
4 |
Correct |
5 ms |
25180 KB |
Output is correct |
5 |
Correct |
5 ms |
25232 KB |
Output is correct |
6 |
Correct |
5 ms |
25248 KB |
Output is correct |
7 |
Correct |
5 ms |
25180 KB |
Output is correct |
8 |
Correct |
5 ms |
25180 KB |
Output is correct |
9 |
Correct |
5 ms |
25180 KB |
Output is correct |
10 |
Correct |
5 ms |
25244 KB |
Output is correct |
11 |
Correct |
6 ms |
25304 KB |
Output is correct |
12 |
Correct |
5 ms |
25180 KB |
Output is correct |
13 |
Correct |
5 ms |
25180 KB |
Output is correct |
14 |
Correct |
6 ms |
25264 KB |
Output is correct |
15 |
Correct |
6 ms |
25436 KB |
Output is correct |
16 |
Correct |
6 ms |
25436 KB |
Output is correct |
17 |
Correct |
7 ms |
25436 KB |
Output is correct |
18 |
Correct |
5 ms |
25180 KB |
Output is correct |
19 |
Correct |
5 ms |
25180 KB |
Output is correct |
20 |
Correct |
7 ms |
25316 KB |
Output is correct |
21 |
Correct |
5 ms |
25432 KB |
Output is correct |
22 |
Correct |
6 ms |
25180 KB |
Output is correct |
23 |
Correct |
6 ms |
25180 KB |
Output is correct |
24 |
Correct |
7 ms |
25288 KB |
Output is correct |
25 |
Correct |
6 ms |
25316 KB |
Output is correct |
26 |
Correct |
5 ms |
25176 KB |
Output is correct |
27 |
Correct |
5 ms |
25180 KB |
Output is correct |
28 |
Correct |
6 ms |
25436 KB |
Output is correct |
29 |
Correct |
10 ms |
25180 KB |
Output is correct |
30 |
Correct |
6 ms |
25180 KB |
Output is correct |
31 |
Correct |
8 ms |
25180 KB |
Output is correct |
32 |
Correct |
9 ms |
25180 KB |
Output is correct |
33 |
Correct |
15 ms |
25436 KB |
Output is correct |
34 |
Correct |
15 ms |
25436 KB |
Output is correct |
35 |
Correct |
17 ms |
25948 KB |
Output is correct |
36 |
Correct |
7 ms |
25488 KB |
Output is correct |
37 |
Correct |
21 ms |
26040 KB |
Output is correct |
38 |
Correct |
21 ms |
25948 KB |
Output is correct |
39 |
Correct |
20 ms |
25948 KB |
Output is correct |
40 |
Correct |
19 ms |
25948 KB |
Output is correct |
41 |
Correct |
20 ms |
26088 KB |
Output is correct |
42 |
Correct |
9 ms |
25692 KB |
Output is correct |
43 |
Correct |
8 ms |
25692 KB |
Output is correct |
44 |
Correct |
10 ms |
25692 KB |
Output is correct |
45 |
Correct |
48 ms |
26752 KB |
Output is correct |
46 |
Correct |
48 ms |
26712 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
25180 KB |
Output is correct |
2 |
Correct |
5 ms |
25180 KB |
Output is correct |
3 |
Correct |
5 ms |
25180 KB |
Output is correct |
4 |
Correct |
5 ms |
25216 KB |
Output is correct |
5 |
Correct |
5 ms |
25180 KB |
Output is correct |
6 |
Correct |
5 ms |
25180 KB |
Output is correct |
7 |
Correct |
5 ms |
25180 KB |
Output is correct |
8 |
Correct |
5 ms |
25176 KB |
Output is correct |
9 |
Correct |
5 ms |
25180 KB |
Output is correct |
10 |
Correct |
5 ms |
25176 KB |
Output is correct |
11 |
Correct |
9 ms |
25436 KB |
Output is correct |
12 |
Correct |
5 ms |
25180 KB |
Output is correct |
13 |
Correct |
5 ms |
25180 KB |
Output is correct |
14 |
Correct |
6 ms |
25436 KB |
Output is correct |
15 |
Correct |
6 ms |
25292 KB |
Output is correct |
16 |
Correct |
5 ms |
25180 KB |
Output is correct |
17 |
Correct |
7 ms |
25280 KB |
Output is correct |
18 |
Correct |
5 ms |
25180 KB |
Output is correct |
19 |
Correct |
5 ms |
25180 KB |
Output is correct |
20 |
Correct |
5 ms |
25436 KB |
Output is correct |
21 |
Correct |
5 ms |
25180 KB |
Output is correct |
22 |
Correct |
5 ms |
25180 KB |
Output is correct |
23 |
Correct |
6 ms |
25180 KB |
Output is correct |
24 |
Correct |
6 ms |
25436 KB |
Output is correct |
25 |
Correct |
5 ms |
25296 KB |
Output is correct |
26 |
Correct |
6 ms |
25432 KB |
Output is correct |
27 |
Correct |
5 ms |
25180 KB |
Output is correct |
28 |
Correct |
6 ms |
25436 KB |
Output is correct |
29 |
Correct |
12 ms |
25176 KB |
Output is correct |
30 |
Correct |
6 ms |
25180 KB |
Output is correct |
31 |
Correct |
8 ms |
25180 KB |
Output is correct |
32 |
Correct |
7 ms |
25180 KB |
Output is correct |
33 |
Correct |
15 ms |
25484 KB |
Output is correct |
34 |
Correct |
15 ms |
25488 KB |
Output is correct |
35 |
Correct |
15 ms |
25944 KB |
Output is correct |
36 |
Correct |
7 ms |
25436 KB |
Output is correct |
37 |
Correct |
21 ms |
25864 KB |
Output is correct |
38 |
Correct |
20 ms |
25948 KB |
Output is correct |
39 |
Correct |
20 ms |
25944 KB |
Output is correct |
40 |
Correct |
20 ms |
25944 KB |
Output is correct |
41 |
Correct |
20 ms |
25944 KB |
Output is correct |
42 |
Correct |
8 ms |
25692 KB |
Output is correct |
43 |
Correct |
8 ms |
25724 KB |
Output is correct |
44 |
Correct |
8 ms |
25692 KB |
Output is correct |
45 |
Correct |
48 ms |
26772 KB |
Output is correct |
46 |
Correct |
47 ms |
26748 KB |
Output is correct |
47 |
Correct |
62 ms |
26244 KB |
Output is correct |
48 |
Correct |
9 ms |
25948 KB |
Output is correct |
49 |
Correct |
11 ms |
25948 KB |
Output is correct |
50 |
Correct |
8 ms |
25692 KB |
Output is correct |
51 |
Correct |
40 ms |
26716 KB |
Output is correct |
52 |
Correct |
43 ms |
26712 KB |
Output is correct |
53 |
Correct |
15 ms |
25948 KB |
Output is correct |
54 |
Correct |
6 ms |
25180 KB |
Output is correct |
55 |
Correct |
6 ms |
25180 KB |
Output is correct |
56 |
Correct |
12 ms |
26460 KB |
Output is correct |
57 |
Correct |
51 ms |
25688 KB |
Output is correct |
58 |
Correct |
11 ms |
25692 KB |
Output is correct |
59 |
Correct |
14 ms |
25692 KB |
Output is correct |
60 |
Correct |
20 ms |
25844 KB |
Output is correct |
61 |
Correct |
19 ms |
25692 KB |
Output is correct |
62 |
Correct |
25 ms |
26712 KB |
Output is correct |
63 |
Correct |
50 ms |
26972 KB |
Output is correct |
64 |
Correct |
53 ms |
27144 KB |
Output is correct |
65 |
Correct |
202 ms |
27240 KB |
Output is correct |
66 |
Correct |
593 ms |
28072 KB |
Output is correct |
67 |
Correct |
596 ms |
28240 KB |
Output is correct |