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;
using pii = pair<int, int>;
const int M = 3e4 + 5;
vector<int> mp[M+1], B(M+1), P(M+1), D(M+1, 1e9);
set<int> S[M+1];
int main() {
int n, m;
cin >> n >> m;
for(int i=0; i<m; i++) {
cin >> B[i] >> P[i];
if(!S[++B[i]].count(P[i])) mp[B[i]].push_back(P[i]);
S[B[i]].insert(P[i]);
}
priority_queue<pii, vector<pii>, greater<pii> > pq;
D[B[0]] = 0;
pq.push({ 0, B[0] });
while(!pq.empty()) {
auto [d, u] = pq.top();
pq.pop();
if(d != D[u]) continue;
for(int &j : mp[u]) {
int v = u + j;
while(v <= n) {
if(D[v] > d + (v - u) / j) {
D[v] = d + (v - u) / j;
pq.push({ D[v], v });
}
if(S[v].count(j)) break;
v += j;
}
v = u - j;
while(v >= 1) {
if(D[v] > d + (u - v) / j) {
D[v] = d + (u - v) / j;
pq.push({ D[v], v });
}
if(S[v].count(j)) break;
v -= j;
}
}
}
cout << (D[B[1]] == 1e9 ? -1 : D[B[1]]) << '\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... |