#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF = (int) 1e9 + 123;
const ll LINF = (ll) 1e18 + 123;
#define rep(i, s, t) for (auto i = (s); i < (t); ++(i))
#define per(i, s, t) for (auto i = (s); i >= (t); --(i))
#define sz(x) ((int) (x).size())
#define mp make_pair
#define pb push_back
bool mini(auto &x, const auto &y) {
if (y < x) {
x = y;
return 1;
}
return 0;
}
bool maxi(auto &x, const auto &y) {
if (y < x) {
x = y;
return 1;
}
return 0;
}
void run();
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
run();
return 0;
}
const int N = 30303;
const int K = 176;
const int V = N * K;
int n, m;
int b[N], p[N];
int dist[V];
bool used[V];
vector<int> which[N];
priority_queue<pair<int, int>> q;
void go(int v, int u, int len) {
if (mini(dist[u], dist[v] + len)) {
q.push({-dist[u], u});
}
}
void run() {
cin >> n >> m;
rep(i, 0, m) {
cin >> b[i] >> p[i];
which[b[i]].pb(p[i]);
}
fill(dist, dist + V, INF);
dist[b[0]] = 0;
q.push({0, b[0]});
while (sz(q)) {
int v = q.top().second;
q.pop();
if (used[v]) continue;
used[v] = 1;
if (v < n) {
for (int step : which[v]) {
if (step < K) {
go(v, v + step * n, 0);
} else {
rep(j, 1, N) {
int u = v + step * j;
if (u >= n) {
break;
}
go(v, u, j);
}
rep(j, 1, N) {
int u = v - step * j;
if (u <= 0) {
break;
}
go(v, u, j);
}
}
}
} else {
int step = v / n;
int pos = v % n;
if (pos + step < n) {
go(v, step * n + pos + step, 1);
}
if (pos - step >= 0) {
go(v, step * n + pos - step, 1);
}
go(v, pos, 0);
}
}
cout << (dist[b[1]] == INF ? -1 : dist[b[1]]) << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
29164 KB |
Output is correct |
2 |
Correct |
6 ms |
29164 KB |
Output is correct |
3 |
Correct |
6 ms |
29164 KB |
Output is correct |
4 |
Correct |
6 ms |
29164 KB |
Output is correct |
5 |
Correct |
3 ms |
29164 KB |
Output is correct |
6 |
Correct |
6 ms |
29164 KB |
Output is correct |
7 |
Correct |
9 ms |
29164 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
29164 KB |
Output is correct |
2 |
Correct |
3 ms |
29164 KB |
Output is correct |
3 |
Correct |
3 ms |
29164 KB |
Output is correct |
4 |
Correct |
3 ms |
29164 KB |
Output is correct |
5 |
Correct |
6 ms |
29164 KB |
Output is correct |
6 |
Correct |
3 ms |
29164 KB |
Output is correct |
7 |
Correct |
3 ms |
29164 KB |
Output is correct |
8 |
Correct |
9 ms |
29164 KB |
Output is correct |
9 |
Correct |
6 ms |
29164 KB |
Output is correct |
10 |
Correct |
3 ms |
29164 KB |
Output is correct |
11 |
Correct |
3 ms |
29164 KB |
Output is correct |
12 |
Correct |
3 ms |
29164 KB |
Output is correct |
13 |
Correct |
0 ms |
29164 KB |
Output is correct |
14 |
Correct |
3 ms |
29164 KB |
Output is correct |
15 |
Correct |
3 ms |
29164 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
29164 KB |
Output is correct |
2 |
Correct |
3 ms |
29164 KB |
Output is correct |
3 |
Correct |
3 ms |
29164 KB |
Output is correct |
4 |
Correct |
0 ms |
29164 KB |
Output is correct |
5 |
Correct |
3 ms |
29164 KB |
Output is correct |
6 |
Correct |
3 ms |
29164 KB |
Output is correct |
7 |
Correct |
3 ms |
29164 KB |
Output is correct |
8 |
Correct |
0 ms |
29164 KB |
Output is correct |
9 |
Correct |
3 ms |
29164 KB |
Output is correct |
10 |
Correct |
6 ms |
29164 KB |
Output is correct |
11 |
Correct |
6 ms |
29164 KB |
Output is correct |
12 |
Correct |
9 ms |
29164 KB |
Output is correct |
13 |
Correct |
6 ms |
29164 KB |
Output is correct |
14 |
Correct |
9 ms |
29164 KB |
Output is correct |
15 |
Correct |
3 ms |
29164 KB |
Output is correct |
16 |
Correct |
6 ms |
29164 KB |
Output is correct |
17 |
Correct |
9 ms |
29164 KB |
Output is correct |
18 |
Correct |
0 ms |
29164 KB |
Output is correct |
19 |
Correct |
0 ms |
29164 KB |
Output is correct |
20 |
Correct |
6 ms |
29164 KB |
Output is correct |
21 |
Correct |
6 ms |
29164 KB |
Output is correct |
22 |
Correct |
6 ms |
29164 KB |
Output is correct |
23 |
Correct |
3 ms |
29164 KB |
Output is correct |
24 |
Correct |
3 ms |
29164 KB |
Output is correct |
25 |
Correct |
0 ms |
29164 KB |
Output is correct |
26 |
Correct |
0 ms |
29164 KB |
Output is correct |
27 |
Correct |
6 ms |
29164 KB |
Output is correct |
28 |
Correct |
3 ms |
29164 KB |
Output is correct |
29 |
Correct |
6 ms |
29164 KB |
Output is correct |
30 |
Correct |
6 ms |
29164 KB |
Output is correct |
31 |
Correct |
9 ms |
29164 KB |
Output is correct |
32 |
Correct |
9 ms |
29164 KB |
Output is correct |
33 |
Correct |
16 ms |
29164 KB |
Output is correct |
34 |
Correct |
16 ms |
29164 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
29164 KB |
Output is correct |
2 |
Correct |
3 ms |
29164 KB |
Output is correct |
3 |
Correct |
9 ms |
29164 KB |
Output is correct |
4 |
Correct |
6 ms |
29164 KB |
Output is correct |
5 |
Correct |
3 ms |
29164 KB |
Output is correct |
6 |
Correct |
6 ms |
29164 KB |
Output is correct |
7 |
Correct |
6 ms |
29164 KB |
Output is correct |
8 |
Correct |
13 ms |
29164 KB |
Output is correct |
9 |
Correct |
0 ms |
29164 KB |
Output is correct |
10 |
Correct |
3 ms |
29164 KB |
Output is correct |
11 |
Correct |
6 ms |
29164 KB |
Output is correct |
12 |
Correct |
3 ms |
29164 KB |
Output is correct |
13 |
Correct |
6 ms |
29164 KB |
Output is correct |
14 |
Correct |
3 ms |
29164 KB |
Output is correct |
15 |
Correct |
3 ms |
29164 KB |
Output is correct |
16 |
Correct |
0 ms |
29164 KB |
Output is correct |
17 |
Correct |
6 ms |
29164 KB |
Output is correct |
18 |
Correct |
9 ms |
29164 KB |
Output is correct |
19 |
Correct |
3 ms |
29164 KB |
Output is correct |
20 |
Correct |
6 ms |
29164 KB |
Output is correct |
21 |
Correct |
0 ms |
29164 KB |
Output is correct |
22 |
Correct |
3 ms |
29164 KB |
Output is correct |
23 |
Correct |
6 ms |
29164 KB |
Output is correct |
24 |
Correct |
6 ms |
29164 KB |
Output is correct |
25 |
Correct |
6 ms |
29164 KB |
Output is correct |
26 |
Correct |
6 ms |
29164 KB |
Output is correct |
27 |
Correct |
3 ms |
29164 KB |
Output is correct |
28 |
Correct |
6 ms |
29164 KB |
Output is correct |
29 |
Correct |
9 ms |
29164 KB |
Output is correct |
30 |
Correct |
6 ms |
29164 KB |
Output is correct |
31 |
Correct |
13 ms |
29164 KB |
Output is correct |
32 |
Correct |
6 ms |
29164 KB |
Output is correct |
33 |
Correct |
19 ms |
29164 KB |
Output is correct |
34 |
Correct |
16 ms |
29164 KB |
Output is correct |
35 |
Correct |
16 ms |
29428 KB |
Output is correct |
36 |
Correct |
3 ms |
29164 KB |
Output is correct |
37 |
Correct |
26 ms |
29448 KB |
Output is correct |
38 |
Correct |
23 ms |
29428 KB |
Output is correct |
39 |
Correct |
23 ms |
29428 KB |
Output is correct |
40 |
Correct |
26 ms |
29428 KB |
Output is correct |
41 |
Correct |
19 ms |
29428 KB |
Output is correct |
42 |
Correct |
3 ms |
29328 KB |
Output is correct |
43 |
Correct |
19 ms |
29328 KB |
Output is correct |
44 |
Correct |
13 ms |
29324 KB |
Output is correct |
45 |
Correct |
59 ms |
29768 KB |
Output is correct |
46 |
Correct |
53 ms |
29764 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
29164 KB |
Output is correct |
2 |
Correct |
0 ms |
29164 KB |
Output is correct |
3 |
Correct |
3 ms |
29164 KB |
Output is correct |
4 |
Correct |
0 ms |
29164 KB |
Output is correct |
5 |
Correct |
3 ms |
29164 KB |
Output is correct |
6 |
Correct |
3 ms |
29164 KB |
Output is correct |
7 |
Correct |
3 ms |
29164 KB |
Output is correct |
8 |
Correct |
3 ms |
29164 KB |
Output is correct |
9 |
Correct |
3 ms |
29164 KB |
Output is correct |
10 |
Correct |
0 ms |
29164 KB |
Output is correct |
11 |
Correct |
13 ms |
29164 KB |
Output is correct |
12 |
Correct |
6 ms |
29164 KB |
Output is correct |
13 |
Correct |
6 ms |
29164 KB |
Output is correct |
14 |
Correct |
3 ms |
29164 KB |
Output is correct |
15 |
Correct |
3 ms |
29164 KB |
Output is correct |
16 |
Correct |
6 ms |
29164 KB |
Output is correct |
17 |
Correct |
0 ms |
29164 KB |
Output is correct |
18 |
Correct |
3 ms |
29164 KB |
Output is correct |
19 |
Correct |
3 ms |
29164 KB |
Output is correct |
20 |
Correct |
3 ms |
29164 KB |
Output is correct |
21 |
Correct |
3 ms |
29164 KB |
Output is correct |
22 |
Correct |
6 ms |
29164 KB |
Output is correct |
23 |
Correct |
3 ms |
29164 KB |
Output is correct |
24 |
Correct |
0 ms |
29164 KB |
Output is correct |
25 |
Correct |
3 ms |
29164 KB |
Output is correct |
26 |
Correct |
6 ms |
29164 KB |
Output is correct |
27 |
Correct |
0 ms |
29164 KB |
Output is correct |
28 |
Correct |
6 ms |
29164 KB |
Output is correct |
29 |
Correct |
6 ms |
29164 KB |
Output is correct |
30 |
Correct |
9 ms |
29164 KB |
Output is correct |
31 |
Correct |
0 ms |
29164 KB |
Output is correct |
32 |
Correct |
6 ms |
29164 KB |
Output is correct |
33 |
Correct |
16 ms |
29164 KB |
Output is correct |
34 |
Correct |
23 ms |
29164 KB |
Output is correct |
35 |
Correct |
19 ms |
29428 KB |
Output is correct |
36 |
Correct |
6 ms |
29164 KB |
Output is correct |
37 |
Correct |
23 ms |
29448 KB |
Output is correct |
38 |
Correct |
23 ms |
29428 KB |
Output is correct |
39 |
Correct |
26 ms |
29428 KB |
Output is correct |
40 |
Correct |
26 ms |
29428 KB |
Output is correct |
41 |
Correct |
29 ms |
29428 KB |
Output is correct |
42 |
Correct |
9 ms |
29328 KB |
Output is correct |
43 |
Correct |
6 ms |
29328 KB |
Output is correct |
44 |
Correct |
16 ms |
29324 KB |
Output is correct |
45 |
Correct |
49 ms |
29768 KB |
Output is correct |
46 |
Correct |
56 ms |
29764 KB |
Output is correct |
47 |
Correct |
93 ms |
29932 KB |
Output is correct |
48 |
Correct |
9 ms |
29560 KB |
Output is correct |
49 |
Correct |
6 ms |
29560 KB |
Output is correct |
50 |
Correct |
6 ms |
29428 KB |
Output is correct |
51 |
Correct |
46 ms |
30180 KB |
Output is correct |
52 |
Correct |
59 ms |
30184 KB |
Output is correct |
53 |
Correct |
13 ms |
29692 KB |
Output is correct |
54 |
Correct |
0 ms |
29164 KB |
Output is correct |
55 |
Correct |
0 ms |
29164 KB |
Output is correct |
56 |
Correct |
9 ms |
30088 KB |
Output is correct |
57 |
Correct |
66 ms |
29164 KB |
Output is correct |
58 |
Correct |
9 ms |
29324 KB |
Output is correct |
59 |
Correct |
16 ms |
29324 KB |
Output is correct |
60 |
Correct |
23 ms |
29332 KB |
Output is correct |
61 |
Correct |
9 ms |
29340 KB |
Output is correct |
62 |
Correct |
19 ms |
30088 KB |
Output is correct |
63 |
Correct |
29 ms |
30200 KB |
Output is correct |
64 |
Correct |
29 ms |
30176 KB |
Output is correct |
65 |
Correct |
233 ms |
30960 KB |
Output is correct |
66 |
Correct |
753 ms |
30916 KB |
Output is correct |
67 |
Correct |
783 ms |
30916 KB |
Output is correct |