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 int long long
const int inf = 1e9;
void solve() {
int n, m;
cin >> n >> m;
vector<set<int>> doges(n);
vector<pair<int, int>> doge(m);
for (auto &[b, p] : doge) {
cin >> b >> p;
doges[b].insert(p);
}
vector<int> d(n, inf);
vector<int> p_a(n, -1);
d[doge[0].first] = 0;
set<pair<int, int>> st;
st.insert({0, doge[0].first});
while (!st.empty()) {
int v = st.begin()->second;
if (v == doge[1].first) {
cout << d[v];
return;
}
st.erase(st.begin());
for (auto &p : doges[v]) {
for (int mul = 1; v + p * mul < n; mul++) {
int nd = d[v] + mul;
int r = v + p * mul;
if (nd < d[r]) {
st.erase({d[r], r});
d[r] = nd;
p_a[r] = p;
st.insert({d[r], r});
} else if (p_a[r] == p) {
break;
}
}
for (int mul = 1; v - p * mul >= 0; mul++) {
int nd = d[v] + mul;
int l = v - p * mul;
if (nd < d[l]) {
st.erase({d[l], l});
d[l] = nd;
p_a[l] = p;
st.insert({d[l], l});
} else if (p_a[l] == p) {
break;
}
}
}
}
cout << -1;
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
solve();
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... |