이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |