#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}, x});
}
}
}
}
dijkstra ();
cout << d[b[1]][0] << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
63 ms |
123304 KB |
Output is correct |
2 |
Correct |
63 ms |
123240 KB |
Output is correct |
3 |
Correct |
65 ms |
123220 KB |
Output is correct |
4 |
Correct |
54 ms |
123260 KB |
Output is correct |
5 |
Correct |
55 ms |
123240 KB |
Output is correct |
6 |
Correct |
56 ms |
123216 KB |
Output is correct |
7 |
Correct |
62 ms |
123320 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
56 ms |
123296 KB |
Output is correct |
2 |
Correct |
61 ms |
123296 KB |
Output is correct |
3 |
Correct |
62 ms |
123248 KB |
Output is correct |
4 |
Correct |
60 ms |
123268 KB |
Output is correct |
5 |
Correct |
66 ms |
123332 KB |
Output is correct |
6 |
Correct |
57 ms |
123292 KB |
Output is correct |
7 |
Correct |
57 ms |
123296 KB |
Output is correct |
8 |
Correct |
60 ms |
123284 KB |
Output is correct |
9 |
Incorrect |
71 ms |
123388 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
59 ms |
123192 KB |
Output is correct |
2 |
Correct |
58 ms |
123292 KB |
Output is correct |
3 |
Correct |
57 ms |
123220 KB |
Output is correct |
4 |
Correct |
57 ms |
123324 KB |
Output is correct |
5 |
Correct |
61 ms |
123320 KB |
Output is correct |
6 |
Correct |
69 ms |
123316 KB |
Output is correct |
7 |
Correct |
61 ms |
123204 KB |
Output is correct |
8 |
Correct |
58 ms |
123356 KB |
Output is correct |
9 |
Incorrect |
61 ms |
123276 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
69 ms |
123208 KB |
Output is correct |
2 |
Correct |
57 ms |
123304 KB |
Output is correct |
3 |
Correct |
58 ms |
123264 KB |
Output is correct |
4 |
Correct |
57 ms |
123308 KB |
Output is correct |
5 |
Correct |
65 ms |
123264 KB |
Output is correct |
6 |
Correct |
59 ms |
123292 KB |
Output is correct |
7 |
Correct |
65 ms |
123204 KB |
Output is correct |
8 |
Correct |
58 ms |
123344 KB |
Output is correct |
9 |
Incorrect |
59 ms |
123344 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
56 ms |
123268 KB |
Output is correct |
2 |
Correct |
60 ms |
123200 KB |
Output is correct |
3 |
Correct |
63 ms |
123220 KB |
Output is correct |
4 |
Correct |
60 ms |
123332 KB |
Output is correct |
5 |
Correct |
58 ms |
123228 KB |
Output is correct |
6 |
Correct |
59 ms |
123260 KB |
Output is correct |
7 |
Correct |
56 ms |
123292 KB |
Output is correct |
8 |
Correct |
56 ms |
123332 KB |
Output is correct |
9 |
Incorrect |
61 ms |
123332 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |