#include <bits/stdc++.h>
using namespace std;
#define FAST_IO ios_base::sync_with_stdio(0); cin.tie(nullptr)
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define REP(n) FOR(O, 1, (n))
#define pb push_back
#define f first
#define s second
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vii;
const int MAXN = 30100;
const int MAXC = (int) (sqrt(MAXN));
int n, m, c;
int b[MAXN], p[MAXN];
vi jumps[MAXN];
int d[MAXN][MAXC];
bool vis[MAXN][MAXC];
vector<pair<pii, int>> adj[MAXN][MAXC];
void dijkstra () {
FOR(i, 0, n-1) FOR(j, 0, c) d[i][j] = -1;
d[b[0]][0] = 0;
priority_queue<pair<int, pii>, vector<pair<int, pii>>, greater<pair<int, pii>>> q;
q.push({0, {b[0], 0}});
while (!q.empty()) {
auto v = q.top().s;
q.pop();
int pos = v.f, j = v.s;
if (vis[pos][j]) continue;
vis[pos][j] = true;
//cout << " pos = " << pos << " j = "<< j << endl;
for (auto e : adj[pos][j]) {
int w = e.s;
int nPos = e.f.f, nJ = e.f.s;
if (d[nPos][nJ] == -1 || d[nPos][nJ] > d[pos][j] + w) {
d[nPos][nJ] = d[pos][j] + w;
q.push({d[nPos][nJ], {nPos, nJ}});
}
}
}
}
int main()
{
FAST_IO;
cin >> n >> m;
c = sqrt(n);
FOR(i, 0, m-1) cin >> b[i] >> p[i];
FOR(i, 0, m-1) jumps[b[i]].pb(p[i]);
FOR(pos, 0, n-1) {
FOR(j, 0, c) {
adj[pos][j].pb({{pos,0}, 0});
if (pos+j < n)
adj[pos][j].pb({{pos+j, j}, 1});
if (pos-j >= 0)
adj[pos][j].pb({{pos-j, j}, 1});
}
for (int j : jumps[pos]) {
if (j <= c) {
adj[pos][0].pb({{pos, j}, 0});
} else {
FOR(x, -c, c) {
int newP = pos + x * j;
if (0 <= newP && newP < n)
adj[pos][0].pb({{newP, 0}, abs(x)});
}
}
}
}
dijkstra ();
cout << d[b[1]][0] << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
59 ms |
123256 KB |
Output is correct |
2 |
Correct |
60 ms |
123220 KB |
Output is correct |
3 |
Correct |
58 ms |
123244 KB |
Output is correct |
4 |
Correct |
59 ms |
123224 KB |
Output is correct |
5 |
Correct |
66 ms |
123316 KB |
Output is correct |
6 |
Correct |
57 ms |
123248 KB |
Output is correct |
7 |
Correct |
57 ms |
123256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
54 ms |
123220 KB |
Output is correct |
2 |
Correct |
65 ms |
123300 KB |
Output is correct |
3 |
Correct |
61 ms |
123320 KB |
Output is correct |
4 |
Correct |
59 ms |
123292 KB |
Output is correct |
5 |
Correct |
62 ms |
123276 KB |
Output is correct |
6 |
Correct |
60 ms |
123312 KB |
Output is correct |
7 |
Correct |
82 ms |
123284 KB |
Output is correct |
8 |
Correct |
59 ms |
123340 KB |
Output is correct |
9 |
Correct |
59 ms |
123348 KB |
Output is correct |
10 |
Correct |
59 ms |
123472 KB |
Output is correct |
11 |
Correct |
58 ms |
123556 KB |
Output is correct |
12 |
Correct |
70 ms |
123524 KB |
Output is correct |
13 |
Correct |
58 ms |
123432 KB |
Output is correct |
14 |
Correct |
66 ms |
123504 KB |
Output is correct |
15 |
Correct |
58 ms |
123520 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
79 ms |
123308 KB |
Output is correct |
2 |
Correct |
59 ms |
123304 KB |
Output is correct |
3 |
Correct |
62 ms |
123260 KB |
Output is correct |
4 |
Correct |
65 ms |
123204 KB |
Output is correct |
5 |
Correct |
68 ms |
123320 KB |
Output is correct |
6 |
Correct |
66 ms |
123296 KB |
Output is correct |
7 |
Correct |
58 ms |
123200 KB |
Output is correct |
8 |
Correct |
62 ms |
123320 KB |
Output is correct |
9 |
Correct |
58 ms |
123292 KB |
Output is correct |
10 |
Correct |
58 ms |
123492 KB |
Output is correct |
11 |
Correct |
59 ms |
123592 KB |
Output is correct |
12 |
Correct |
62 ms |
123540 KB |
Output is correct |
13 |
Correct |
59 ms |
123460 KB |
Output is correct |
14 |
Correct |
58 ms |
123544 KB |
Output is correct |
15 |
Correct |
63 ms |
123612 KB |
Output is correct |
16 |
Correct |
59 ms |
123684 KB |
Output is correct |
17 |
Correct |
79 ms |
125380 KB |
Output is correct |
18 |
Correct |
86 ms |
129096 KB |
Output is correct |
19 |
Correct |
68 ms |
130252 KB |
Output is correct |
20 |
Correct |
68 ms |
130628 KB |
Output is correct |
21 |
Correct |
70 ms |
124072 KB |
Output is correct |
22 |
Correct |
78 ms |
129232 KB |
Output is correct |
23 |
Correct |
67 ms |
128708 KB |
Output is correct |
24 |
Correct |
78 ms |
130312 KB |
Output is correct |
25 |
Correct |
69 ms |
130620 KB |
Output is correct |
26 |
Correct |
71 ms |
130668 KB |
Output is correct |
27 |
Correct |
87 ms |
130648 KB |
Output is correct |
28 |
Correct |
76 ms |
130712 KB |
Output is correct |
29 |
Correct |
83 ms |
130604 KB |
Output is correct |
30 |
Correct |
78 ms |
130500 KB |
Output is correct |
31 |
Correct |
75 ms |
130568 KB |
Output is correct |
32 |
Correct |
70 ms |
130884 KB |
Output is correct |
33 |
Correct |
109 ms |
131396 KB |
Output is correct |
34 |
Correct |
88 ms |
131368 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
57 ms |
123264 KB |
Output is correct |
2 |
Correct |
57 ms |
123216 KB |
Output is correct |
3 |
Correct |
59 ms |
123304 KB |
Output is correct |
4 |
Correct |
57 ms |
123228 KB |
Output is correct |
5 |
Correct |
60 ms |
123340 KB |
Output is correct |
6 |
Correct |
63 ms |
123204 KB |
Output is correct |
7 |
Correct |
57 ms |
123316 KB |
Output is correct |
8 |
Correct |
62 ms |
123280 KB |
Output is correct |
9 |
Correct |
63 ms |
123352 KB |
Output is correct |
10 |
Correct |
70 ms |
123488 KB |
Output is correct |
11 |
Correct |
58 ms |
123592 KB |
Output is correct |
12 |
Correct |
60 ms |
123496 KB |
Output is correct |
13 |
Correct |
60 ms |
123504 KB |
Output is correct |
14 |
Correct |
70 ms |
123588 KB |
Output is correct |
15 |
Correct |
67 ms |
123472 KB |
Output is correct |
16 |
Correct |
59 ms |
123696 KB |
Output is correct |
17 |
Correct |
61 ms |
125528 KB |
Output is correct |
18 |
Correct |
76 ms |
129248 KB |
Output is correct |
19 |
Correct |
71 ms |
130308 KB |
Output is correct |
20 |
Correct |
70 ms |
130668 KB |
Output is correct |
21 |
Correct |
58 ms |
124100 KB |
Output is correct |
22 |
Correct |
65 ms |
129312 KB |
Output is correct |
23 |
Correct |
67 ms |
128708 KB |
Output is correct |
24 |
Correct |
77 ms |
130200 KB |
Output is correct |
25 |
Correct |
76 ms |
130628 KB |
Output is correct |
26 |
Correct |
72 ms |
130692 KB |
Output is correct |
27 |
Correct |
71 ms |
130608 KB |
Output is correct |
28 |
Correct |
81 ms |
130816 KB |
Output is correct |
29 |
Correct |
104 ms |
130580 KB |
Output is correct |
30 |
Correct |
78 ms |
130560 KB |
Output is correct |
31 |
Correct |
75 ms |
130620 KB |
Output is correct |
32 |
Correct |
71 ms |
130900 KB |
Output is correct |
33 |
Correct |
85 ms |
131412 KB |
Output is correct |
34 |
Correct |
83 ms |
131428 KB |
Output is correct |
35 |
Correct |
87 ms |
129732 KB |
Output is correct |
36 |
Correct |
91 ms |
126744 KB |
Output is correct |
37 |
Correct |
91 ms |
132908 KB |
Output is correct |
38 |
Correct |
93 ms |
133208 KB |
Output is correct |
39 |
Correct |
94 ms |
133268 KB |
Output is correct |
40 |
Correct |
93 ms |
133372 KB |
Output is correct |
41 |
Correct |
95 ms |
133252 KB |
Output is correct |
42 |
Correct |
80 ms |
131580 KB |
Output is correct |
43 |
Correct |
74 ms |
131504 KB |
Output is correct |
44 |
Correct |
77 ms |
131644 KB |
Output is correct |
45 |
Correct |
97 ms |
136728 KB |
Output is correct |
46 |
Correct |
96 ms |
136768 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
59 ms |
123204 KB |
Output is correct |
2 |
Correct |
62 ms |
123316 KB |
Output is correct |
3 |
Correct |
65 ms |
123248 KB |
Output is correct |
4 |
Correct |
61 ms |
123212 KB |
Output is correct |
5 |
Correct |
59 ms |
123240 KB |
Output is correct |
6 |
Correct |
58 ms |
123224 KB |
Output is correct |
7 |
Correct |
58 ms |
123312 KB |
Output is correct |
8 |
Correct |
59 ms |
123240 KB |
Output is correct |
9 |
Correct |
62 ms |
123308 KB |
Output is correct |
10 |
Correct |
62 ms |
123460 KB |
Output is correct |
11 |
Correct |
68 ms |
123588 KB |
Output is correct |
12 |
Correct |
59 ms |
123452 KB |
Output is correct |
13 |
Correct |
58 ms |
123520 KB |
Output is correct |
14 |
Correct |
65 ms |
123584 KB |
Output is correct |
15 |
Correct |
67 ms |
123588 KB |
Output is correct |
16 |
Correct |
59 ms |
123652 KB |
Output is correct |
17 |
Correct |
63 ms |
125416 KB |
Output is correct |
18 |
Correct |
66 ms |
129152 KB |
Output is correct |
19 |
Correct |
69 ms |
130244 KB |
Output is correct |
20 |
Correct |
70 ms |
130548 KB |
Output is correct |
21 |
Correct |
63 ms |
124100 KB |
Output is correct |
22 |
Correct |
68 ms |
129220 KB |
Output is correct |
23 |
Correct |
67 ms |
128692 KB |
Output is correct |
24 |
Correct |
85 ms |
130220 KB |
Output is correct |
25 |
Correct |
94 ms |
130652 KB |
Output is correct |
26 |
Correct |
70 ms |
130636 KB |
Output is correct |
27 |
Correct |
71 ms |
130628 KB |
Output is correct |
28 |
Correct |
76 ms |
130784 KB |
Output is correct |
29 |
Correct |
80 ms |
130528 KB |
Output is correct |
30 |
Correct |
72 ms |
130588 KB |
Output is correct |
31 |
Correct |
85 ms |
130568 KB |
Output is correct |
32 |
Correct |
98 ms |
130888 KB |
Output is correct |
33 |
Correct |
88 ms |
131412 KB |
Output is correct |
34 |
Correct |
85 ms |
131340 KB |
Output is correct |
35 |
Correct |
85 ms |
129816 KB |
Output is correct |
36 |
Correct |
66 ms |
126848 KB |
Output is correct |
37 |
Correct |
95 ms |
132940 KB |
Output is correct |
38 |
Correct |
96 ms |
133348 KB |
Output is correct |
39 |
Correct |
90 ms |
133280 KB |
Output is correct |
40 |
Correct |
94 ms |
133268 KB |
Output is correct |
41 |
Correct |
98 ms |
133352 KB |
Output is correct |
42 |
Correct |
88 ms |
131424 KB |
Output is correct |
43 |
Correct |
79 ms |
131496 KB |
Output is correct |
44 |
Correct |
84 ms |
131592 KB |
Output is correct |
45 |
Correct |
100 ms |
136744 KB |
Output is correct |
46 |
Correct |
118 ms |
136704 KB |
Output is correct |
47 |
Correct |
389 ms |
227504 KB |
Output is correct |
48 |
Runtime error |
269 ms |
262148 KB |
Execution killed with signal 9 |
49 |
Halted |
0 ms |
0 KB |
- |