이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 30000;
vector<pair<int, int>> adj[N];
int b[N], p[N];
set<int> inv[N];
int n, m;
void solveTestCase() {
cin >> n >> m;
for (int i = 0; i < m; i++) {
cin >> b[i] >> p[i];
inv[b[i]].insert(p[i]);
}
for (int i = 0; i < m; i++) {
for (int j = b[i] - p[i], k = 1; j >= 0; j -= p[i], k++) {
adj[b[i]].emplace_back(j, k);
if (inv[j].count(p[i]))
break;
}
for (int j = b[i] + p[i], k = 1; j < n; j += p[i], k++) {
adj[b[i]].emplace_back(j, k);
if (inv[j].count(p[i]))
break;
}
}
/*for (int i = 0; i < n; i++) {
for (int j : inv[i]) {
for (int k = i - j, l = 1; k >= 0; k -= j, l++) {
adj[i].emplace_back(k, l);
if (inv[k].count(j))
break;
}
for (int k = i + j, l = 1; k < n; k += j, l++) {
adj[i].emplace_back(k, l);
if (inv[k].count(j))
break;
}
}
}*/
int dist[n];
fill(dist, dist + n, 1e18);
dist[b[0]] = 0;
priority_queue<pair<int, int>, vector<pair<int, int>>, decltype(greater<>())> pq;
pq.push({0, b[0]});
set<int> done;
while (!pq.empty()) {
auto temp = pq.top();
pq.pop();
if (done.count(temp.second))
continue;
done.insert(temp.second);
assert(temp.first == dist[temp.second] && temp.first != 1e18);
for (auto i : adj[temp.second]) {
if (dist[i.first] > temp.first + i.second) {
dist[i.first] = temp.first + i.second;
pq.push({dist[i.first], i.first});
}
}
}
cout << (dist[b[1]] == 1e18 ? -1 : dist[b[1]]);
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int test = 1;
// cin >> test;
while (test--)
solveTestCase();
}
# | 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... |