이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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] + p[i]; j < n; j += p[i]) {
cnt++;
for (auto to : v[j]) ed[i][to] = min(ed[i][to], cnt);
}
cnt = 0;
for (int j = b[i] - p[i]; j >= 0; j -= p[i]){
cnt++;
for (auto to : v[j]) ed[i][to] = min(ed[i][to], 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... |