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;
const int INF = 0x3f3f3f3f;
typedef pair<int, int> pi;
#define to first
#define cost second
#define pos first
#define range second
struct Q {
int pos, cost;
};
bool operator<(const Q& a, const Q& b) {
return a.cost > b.cost;
}
set<pi> V[30001];
set<int> inpos[30001];
int dst[30001];
int main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
memset(dst, INF, sizeof(dst));
int n, m; cin >> n >> m;
vector<vector<int>> doge(n);
int s = 0, e = 0;
for (int i = 0; i < m; i++)
{
int pos, range; cin >> pos >> range;
if (i == 0) s = pos;
if (i == 1) e = pos;
doge[pos].push_back(range);
inpos[pos].insert(range);
}
for (int i = 0; i < n; i++) {
sort(doge[i].begin(), doge[i].end());
doge[i].erase(unique(doge[i].begin(), doge[i].end()), doge[i].end());
}
for (int pos = 0; pos < n; pos++) {
set<pi>& nV = V[pos];
for (int range : doge[pos]) {
for (int j = pos + range; j < n; j += range) {
if (inpos[j].size() == 0) continue;
int dst = j - pos;
nV.insert({ j, dst / range });
if (inpos[j].find(range) != inpos[j].end()) break;
}
for (int j = pos - range; j >= 0; j -= range) {
if (inpos[j].size() == 0) continue;
int dst = pos - j;
nV.insert({ j, dst / range });
if (inpos[j].find(range) != inpos[j].end()) break;
}
}
}
vector<int> vit(n, -1);
int num = 0;
priority_queue<Q> pq;
pq.push({ s, 0 });
while (!pq.empty()) {
Q top = pq.top(); pq.pop();
if (top.pos == e) {
cout << top.cost;
return 0;
}
for (pi w : V[top.pos]) {
if (vit[w.to] == num) continue;
if (dst[w.to] > top.cost + w.cost) {
dst[w.to] = top.cost + w.cost;
pq.push({ w.to, dst[w.to] });
}
vit[w.to] = num;
}
num++;
}
cout << -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... |