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>
#define ll long long
#define all(aaa) aaa.begin(), aaa.end()
using namespace std;
struct E {
int to, w;
};
const int N = 3e4 + 5, P = 200, V = N + N + N * P, INF = 1e9;
vector<E> g[N + N];
int d[V];
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
int n, m;
cin >> n >> m;
for (int i = 0; i < m; i++) {
int x, y;
cin >> x >> y;
g[m + x].push_back({i, 0});
g[i].push_back({m + x, 0});
if (y < P) {
g[i].push_back({n + m + x * P + y, 0});
}
else {
int z = x - y, w = 1;
while (z >= 0) {
g[i].push_back({m + z, w});
z -= y;
w++;
}
z = x + y, w = 1;
while (z < n) {
g[i].push_back({m + z, w});
z += y;
w++;
}
}
}
// for (int i = 0; i < n; i++) {
// for (int j = 1; j < P; j++) {
// int ver = n + m + i * P + j;
// g[ver].push_back({m + i, 0});
// if (i - j >= 0) {
// g[ver].push_back({ver - j * P, 1});
// }
// if (i + j < n) {
// g[ver].push_back({ver + j * P, 1});
// }
// }
// }
fill(d, d + V, INF);
d[0] = 0;
priority_queue<pair<int, int>,
vector<pair<int, int>>,
greater<pair<int, int>>> q;
q.push({d[0], 0});
while (!q.empty()) {
int node = q.top().second,
cd = q.top().first;
q.pop();
if (cd != d[node])
continue;
if (node < n + m) {
for (E e : g[node]) {
if (d[e.to] > cd + e.w) {
d[e.to] = cd + e.w;
q.push({d[e.to], e.to});
}
}
}
else {
int x = (node - n - m) / P,
y = node - n - m - x * P;
if (d[m + x] > cd) {
d[m + x] = cd;
q.push({d[m + x], m + x});
}
if (x - y >= 0 && d[node - y * P] > cd + 1) {
d[node - y * P] = cd + 1;
q.push({d[node - y * P], node - y * P});
}
if (x + y < n && d[node + y * P] > cd + 1) {
d[node + y * P] = cd + 1;
q.push({d[node + y * P], node + y * P});
}
}
}
cout << (d[1] < INF ? d[1] : -1);
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... |