This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define pb push_back
#define all(x) begin(x), end(x)
#define SZ(x) (int)(x).size()
#define cps(x) sort(all(x)), (x).erase(unique(all(x)), end(x))
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int n0 = 2003, m0 = 30003;
const int k0 = n0 * m0;
int n, m, b[m0], p[m0], k, d[k0], inf;
vector <int> g[n0];
int ind(int x, int y) {
if (y == m) return x;
return n + x * m + y;
}
void rev(int x, int & i, int & j) {
if (x < n) {
i = x, j = m;
return;
}
x -= n;
i = x / m;
j = x - i * m;
}
void bfs(int x, int y) {
memset(& d, 0x3f, sizeof(d));
inf = d[0];
deque <int> q;
d[ind(x, y)] = 0;
q.push_front(ind(x, y));
while (!q.empty()) {
int v = q.front();
q.pop_front();
if (v < n) {
for (int to : g[v]) {
if (d[to] > d[v]) {
d[to] = d[v];
q.push_front(to);
}
}
} else {
int i, j;
rev(v, i, j);
if (i - p[j] >= 0) {
int to = ind(i - p[j], j);
if (d[to] > d[v] + 1) {
d[to] = d[v] + 1;
q.pb(to);
}
}
if (i + p[j] < n) {
int to = ind(i + p[j], j);
if (d[to] > d[v] + 1) {
d[to] = d[v] + 1;
q.pb(to);
}
}
int to = ind(i, m);
if (d[to] > d[v]) {
d[to] = d[v];
q.push_front(to);
}
}
}
}
int main() {
ios_base::sync_with_stdio(false), cin.tie(NULL);
cin >> n >> m;
for (int i = 0; i < m; i++)
cin >> b[i] >> p[i];
for (int i = 0; i < m; i++)
g[ind(b[i], m)].pb(ind(b[i], i));
bfs(b[0], 0);
int res = d[ind(b[1], 1)];
if (res == inf) res = -1;
cout << res;
}
# | 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... |