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;
typedef long long ll;
#define int ll
#define pb push_back
const int INF = 1e18;
bool cmin(int &a, const int &b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
void run() {
int n, m;
cin >> n >> m;
vector<int> b(m), p(m);
vector<vector<int>> who(n);
for (int i = 0; i < m; ++i) {
cin >> b[i] >> p[i];
who[b[i]].pb(i);
}
vector<int> dist(n, INF);
queue<int> q;
q.push(0);
dist[b[0]] = 0;
while (!q.empty()) {
int u = q.front();
q.pop();
for (auto x : who[u]) {
int cnt = 0;
for (int i = u; i < n; i += p[x]) {
if (cmin(dist[i], dist[u] + cnt)) {
q.push(i);
}
++cnt;
}
cnt = 0;
for (int i = u; i >= 0; i -= p[x]) {
if (cmin(dist[i], dist[u] + cnt)) {
q.push(i);
}
++cnt;
}
}
}
if (dist[b[1]] >= INF)
dist[b[1]] = -1;
cout << dist[b[1]] << '\n';
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
run();
}
# | 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... |