이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define sp ' '
#define en '\n'
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
using namespace std;
const int N = 3e4 + 2;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, m;
cin >> n >> m;
vector<vector<int>> g(n);
int x = -1; int y = -1;
for (int i = 0; i < m; i++) {
int b, p;
cin >> b >> p;
if (i == 0) x = b;
if (i == 1) y = b;
g[b].push_back(p);
}
priority_queue<pair<int, int>> q;
vector<int> dist(n, 1e9);
vector<bool> was(n);
q.push({0, x});
dist[x] = 0;
while (!q.empty()) {
int s = q.top().second;
q.pop();
if (was[s] == 1) continue;
was[s] = 1;
for (int d : g[s]) {
int tez = 1;
for (int i = s - d; i >= 0; i -= d) {
if (dist[i] > dist[s] + tez) {
dist[i] = dist[s] + tez;
q.push({-dist[i], i});
}
tez += 1;
}
tez = 1;
for (int i = s + d; i < n; i += d) {
if (dist[i] > dist[s] + tez) {
dist[i] = dist[s] + tez;
q.push({-dist[i], i});
}
tez += 1;
}
}
}
if (dist[y] == 1e9) dist[y] = -1;
cout << dist[y] << en;
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... |