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 oo 1000000000000000000ll
#define int long long
signed main() {
int n, m; cin >> n >> m;
vector<int> b(m), p(m);
for (int i = 0; i < m; ++i) cin >> b[i] >> p[i];
vector<vector<int>> v(n);
for (int i = 0; i < m; ++i) v[b[i]].push_back(i);
vector<vector<int>> ed(m, vector<int>(m, oo));
for (int i = 0; i < m; ++i){
int cnt = 0;
for (int j = b[i]; j < n; j += p[i]) {
for (auto to : v[j]) ed[i][to] = min(ed[i][to], cnt);
cnt++;
}
cnt = 0;
for (int j = b[i]; j >= 0; j -= p[i]){
for (auto to : v[j]) ed[i][to] = min(ed[i][to], cnt);
cnt++;
}
}
vector<int> d(m, oo); d[0] = 0;
vector<bool> visited(m);
for (int i = 0; i < m; ++i){
int mn = -1;
for (int j = 0; j < m; ++j){
if (!visited[j] && (mn == -1 || d[mn] > d[j])) mn = j;
}
if (mn == -1) break;
visited[mn] = true;
for (int j = 0; j < m; ++j){
if (visited[j]) continue;
if (d[j] > d[mn] + ed[mn][j]) d[j] = d[mn] + ed[mn][j];
}
}
int ans = d[1];
if (ans == oo) ans = -1;
cout << ans << "\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... |