#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];
vector<pair<pii, int>> adj;
void calcAdj (int pos, int j) {
adj.clear();
if (j > 0) {
adj.pb({{pos,0}, 0});
if (pos+j < n)
adj.pb({{pos+j, j}, 1});
if (pos-j >= 0)
adj.pb({{pos-j, j}, 1});
} else {
for (int j : jumps[pos]) {
if (j <= c) {
adj.pb({{pos, j}, 0});
} else {
FOR(x, -c, c) {
int newP = pos + x * j;
if (0 <= newP && newP < n)
adj.pb({{newP, 0}, abs(x)});
}
}
}
}
}
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;
calcAdj (pos, j);
for (auto e : adj) {
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 |
1 ms |
972 KB |
Output is correct |
2 |
Correct |
1 ms |
972 KB |
Output is correct |
3 |
Correct |
1 ms |
972 KB |
Output is correct |
4 |
Correct |
1 ms |
972 KB |
Output is correct |
5 |
Correct |
1 ms |
972 KB |
Output is correct |
6 |
Correct |
1 ms |
972 KB |
Output is correct |
7 |
Correct |
1 ms |
972 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
972 KB |
Output is correct |
2 |
Correct |
1 ms |
972 KB |
Output is correct |
3 |
Correct |
1 ms |
972 KB |
Output is correct |
4 |
Correct |
1 ms |
972 KB |
Output is correct |
5 |
Correct |
1 ms |
972 KB |
Output is correct |
6 |
Correct |
1 ms |
972 KB |
Output is correct |
7 |
Correct |
1 ms |
972 KB |
Output is correct |
8 |
Correct |
1 ms |
972 KB |
Output is correct |
9 |
Correct |
1 ms |
972 KB |
Output is correct |
10 |
Correct |
1 ms |
1100 KB |
Output is correct |
11 |
Correct |
1 ms |
1100 KB |
Output is correct |
12 |
Correct |
1 ms |
1100 KB |
Output is correct |
13 |
Correct |
1 ms |
1100 KB |
Output is correct |
14 |
Correct |
1 ms |
1100 KB |
Output is correct |
15 |
Correct |
1 ms |
1100 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
972 KB |
Output is correct |
2 |
Correct |
1 ms |
972 KB |
Output is correct |
3 |
Correct |
1 ms |
972 KB |
Output is correct |
4 |
Correct |
1 ms |
972 KB |
Output is correct |
5 |
Correct |
1 ms |
972 KB |
Output is correct |
6 |
Correct |
1 ms |
972 KB |
Output is correct |
7 |
Correct |
1 ms |
972 KB |
Output is correct |
8 |
Correct |
1 ms |
976 KB |
Output is correct |
9 |
Correct |
1 ms |
972 KB |
Output is correct |
10 |
Correct |
1 ms |
1112 KB |
Output is correct |
11 |
Correct |
1 ms |
1100 KB |
Output is correct |
12 |
Correct |
1 ms |
1100 KB |
Output is correct |
13 |
Correct |
1 ms |
1100 KB |
Output is correct |
14 |
Correct |
2 ms |
1100 KB |
Output is correct |
15 |
Correct |
1 ms |
1100 KB |
Output is correct |
16 |
Correct |
1 ms |
1100 KB |
Output is correct |
17 |
Correct |
2 ms |
1612 KB |
Output is correct |
18 |
Correct |
2 ms |
2252 KB |
Output is correct |
19 |
Correct |
1 ms |
2380 KB |
Output is correct |
20 |
Correct |
3 ms |
2764 KB |
Output is correct |
21 |
Correct |
1 ms |
1228 KB |
Output is correct |
22 |
Correct |
1 ms |
2252 KB |
Output is correct |
23 |
Correct |
2 ms |
2384 KB |
Output is correct |
24 |
Correct |
3 ms |
2636 KB |
Output is correct |
25 |
Correct |
2 ms |
2764 KB |
Output is correct |
26 |
Correct |
2 ms |
2764 KB |
Output is correct |
27 |
Correct |
2 ms |
2764 KB |
Output is correct |
28 |
Correct |
4 ms |
2764 KB |
Output is correct |
29 |
Correct |
9 ms |
2776 KB |
Output is correct |
30 |
Correct |
4 ms |
2636 KB |
Output is correct |
31 |
Correct |
6 ms |
2764 KB |
Output is correct |
32 |
Correct |
3 ms |
2764 KB |
Output is correct |
33 |
Correct |
15 ms |
2956 KB |
Output is correct |
34 |
Correct |
14 ms |
2892 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
972 KB |
Output is correct |
2 |
Correct |
1 ms |
972 KB |
Output is correct |
3 |
Correct |
1 ms |
972 KB |
Output is correct |
4 |
Correct |
1 ms |
972 KB |
Output is correct |
5 |
Correct |
1 ms |
1000 KB |
Output is correct |
6 |
Correct |
1 ms |
972 KB |
Output is correct |
7 |
Correct |
1 ms |
1008 KB |
Output is correct |
8 |
Correct |
1 ms |
972 KB |
Output is correct |
9 |
Correct |
1 ms |
972 KB |
Output is correct |
10 |
Correct |
1 ms |
1100 KB |
Output is correct |
11 |
Correct |
1 ms |
1100 KB |
Output is correct |
12 |
Correct |
1 ms |
1100 KB |
Output is correct |
13 |
Correct |
1 ms |
1100 KB |
Output is correct |
14 |
Correct |
1 ms |
1100 KB |
Output is correct |
15 |
Correct |
2 ms |
1100 KB |
Output is correct |
16 |
Correct |
1 ms |
1100 KB |
Output is correct |
17 |
Correct |
3 ms |
1612 KB |
Output is correct |
18 |
Correct |
2 ms |
2252 KB |
Output is correct |
19 |
Correct |
2 ms |
2380 KB |
Output is correct |
20 |
Correct |
2 ms |
2764 KB |
Output is correct |
21 |
Correct |
1 ms |
1228 KB |
Output is correct |
22 |
Correct |
1 ms |
2252 KB |
Output is correct |
23 |
Correct |
2 ms |
2380 KB |
Output is correct |
24 |
Correct |
3 ms |
2636 KB |
Output is correct |
25 |
Correct |
2 ms |
2764 KB |
Output is correct |
26 |
Correct |
2 ms |
2764 KB |
Output is correct |
27 |
Correct |
2 ms |
2764 KB |
Output is correct |
28 |
Correct |
3 ms |
2764 KB |
Output is correct |
29 |
Correct |
8 ms |
2764 KB |
Output is correct |
30 |
Correct |
4 ms |
2636 KB |
Output is correct |
31 |
Correct |
6 ms |
2764 KB |
Output is correct |
32 |
Correct |
4 ms |
2764 KB |
Output is correct |
33 |
Correct |
13 ms |
2892 KB |
Output is correct |
34 |
Correct |
14 ms |
2912 KB |
Output is correct |
35 |
Correct |
15 ms |
2636 KB |
Output is correct |
36 |
Correct |
3 ms |
1996 KB |
Output is correct |
37 |
Correct |
16 ms |
3108 KB |
Output is correct |
38 |
Correct |
17 ms |
3276 KB |
Output is correct |
39 |
Correct |
16 ms |
3288 KB |
Output is correct |
40 |
Correct |
16 ms |
3296 KB |
Output is correct |
41 |
Correct |
16 ms |
3292 KB |
Output is correct |
42 |
Correct |
6 ms |
3660 KB |
Output is correct |
43 |
Correct |
6 ms |
3660 KB |
Output is correct |
44 |
Correct |
5 ms |
3644 KB |
Output is correct |
45 |
Correct |
20 ms |
3328 KB |
Output is correct |
46 |
Correct |
20 ms |
3276 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
972 KB |
Output is correct |
2 |
Correct |
1 ms |
972 KB |
Output is correct |
3 |
Correct |
1 ms |
972 KB |
Output is correct |
4 |
Correct |
1 ms |
972 KB |
Output is correct |
5 |
Correct |
1 ms |
972 KB |
Output is correct |
6 |
Correct |
1 ms |
972 KB |
Output is correct |
7 |
Correct |
1 ms |
972 KB |
Output is correct |
8 |
Correct |
1 ms |
972 KB |
Output is correct |
9 |
Correct |
1 ms |
972 KB |
Output is correct |
10 |
Correct |
1 ms |
1100 KB |
Output is correct |
11 |
Correct |
1 ms |
1100 KB |
Output is correct |
12 |
Correct |
1 ms |
1100 KB |
Output is correct |
13 |
Correct |
1 ms |
1100 KB |
Output is correct |
14 |
Correct |
1 ms |
1100 KB |
Output is correct |
15 |
Correct |
1 ms |
1100 KB |
Output is correct |
16 |
Correct |
1 ms |
1100 KB |
Output is correct |
17 |
Correct |
3 ms |
1612 KB |
Output is correct |
18 |
Correct |
1 ms |
2252 KB |
Output is correct |
19 |
Correct |
1 ms |
2380 KB |
Output is correct |
20 |
Correct |
2 ms |
2764 KB |
Output is correct |
21 |
Correct |
1 ms |
1228 KB |
Output is correct |
22 |
Correct |
1 ms |
2252 KB |
Output is correct |
23 |
Correct |
3 ms |
2380 KB |
Output is correct |
24 |
Correct |
3 ms |
2636 KB |
Output is correct |
25 |
Correct |
3 ms |
2764 KB |
Output is correct |
26 |
Correct |
3 ms |
2724 KB |
Output is correct |
27 |
Correct |
2 ms |
2764 KB |
Output is correct |
28 |
Correct |
4 ms |
2704 KB |
Output is correct |
29 |
Correct |
9 ms |
2756 KB |
Output is correct |
30 |
Correct |
4 ms |
2636 KB |
Output is correct |
31 |
Correct |
6 ms |
2764 KB |
Output is correct |
32 |
Correct |
3 ms |
2764 KB |
Output is correct |
33 |
Correct |
13 ms |
2892 KB |
Output is correct |
34 |
Correct |
13 ms |
2892 KB |
Output is correct |
35 |
Correct |
12 ms |
2756 KB |
Output is correct |
36 |
Correct |
3 ms |
1996 KB |
Output is correct |
37 |
Correct |
15 ms |
3132 KB |
Output is correct |
38 |
Correct |
17 ms |
3184 KB |
Output is correct |
39 |
Correct |
17 ms |
3280 KB |
Output is correct |
40 |
Correct |
20 ms |
3288 KB |
Output is correct |
41 |
Correct |
19 ms |
3276 KB |
Output is correct |
42 |
Correct |
7 ms |
3648 KB |
Output is correct |
43 |
Correct |
6 ms |
3660 KB |
Output is correct |
44 |
Correct |
5 ms |
3644 KB |
Output is correct |
45 |
Correct |
21 ms |
3400 KB |
Output is correct |
46 |
Correct |
21 ms |
3276 KB |
Output is correct |
47 |
Correct |
87 ms |
12368 KB |
Output is correct |
48 |
Correct |
11 ms |
17636 KB |
Output is correct |
49 |
Correct |
12 ms |
18956 KB |
Output is correct |
50 |
Correct |
11 ms |
20556 KB |
Output is correct |
51 |
Correct |
85 ms |
28076 KB |
Output is correct |
52 |
Correct |
80 ms |
27996 KB |
Output is correct |
53 |
Correct |
35 ms |
27524 KB |
Output is correct |
54 |
Correct |
15 ms |
26316 KB |
Output is correct |
55 |
Correct |
15 ms |
26372 KB |
Output is correct |
56 |
Correct |
22 ms |
27812 KB |
Output is correct |
57 |
Correct |
92 ms |
26436 KB |
Output is correct |
58 |
Correct |
32 ms |
27460 KB |
Output is correct |
59 |
Correct |
34 ms |
27612 KB |
Output is correct |
60 |
Correct |
44 ms |
27504 KB |
Output is correct |
61 |
Correct |
31 ms |
27572 KB |
Output is correct |
62 |
Correct |
72 ms |
27896 KB |
Output is correct |
63 |
Correct |
87 ms |
28496 KB |
Output is correct |
64 |
Correct |
121 ms |
28544 KB |
Output is correct |
65 |
Correct |
280 ms |
28720 KB |
Output is correct |
66 |
Correct |
606 ms |
28688 KB |
Output is correct |
67 |
Correct |
623 ms |
28636 KB |
Output is correct |