이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int M = 300004;
int n, m;
int ans;
int b[M];
int p[M];
int cost[M];
vector<pair<int, int>> g[M];
inline void dij() {
memset(cost, -1, sizeof cost);
cost[b[0]] = 0;
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int >>> pq;
pq.emplace(0, b[0]);
while (!pq.empty()) {
auto src = pq.top();
assert(src.second < M);
pq.pop();
if (cost[src.second] != src.first) {
continue;
}
if (src.second == b[1]) {
ans = min(ans, src.first);
}
for (auto [u, w] : g[src.second]) {
assert(u < M);
if (cost[u] == -1 || cost[u] > src.first + w) {
cost[u] = src.first + w;
pq.emplace(cost[u], u);
}
}
}
}
signed main() {
ans = INT_MAX;
cin >> n >> m;
for (int i = 0; i < m; ++i) {
cin >> b[i] >> p[i];
assert(b[i] < M);
for (int j = 1; j < n; ++j) {
int r = b[i] + j * p[i];
int l = b[i] - j * p[i];
if (l >= 0){
g[b[i]].emplace_back(l, j);
}
if (r < n) {
g[b[i]].emplace_back(r, j);
}
}
}
dij();
cout << (ans == INT_MAX ? -1 : 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... |