이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
typedef pair<int, int> pi;
#define to first
#define cost second
#define pos first
#define range second
struct Q {
int pos, cost;
};
bool operator<(const Q& a, const Q& b) {
return a.cost > b.cost;
}
set<pi> V[30001];
set<int> vitP[30001];
int dst[30001];
int main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
memset(dst, INF, sizeof(dst));
int n, m; cin >> n >> m;
vector<pi> doge(m);
vector<int> posdoge(n + 1, -1);
int s = 0, e = 0;
for (int i = 0; i < m; i++) {
cin >> doge[i].pos >> doge[i].range;
if (i == 0) s = doge[i].pos;
if (i == 1) e = doge[i].pos;
vitP[doge[i].pos].insert(doge[i].range);
}
for (int i = 0; i < m; i++) {
pi& now = doge[i];
for (int j = now.pos + now.range; j < n; j += now.range) {
if (vitP[j].size() == 0) continue;
int dst = j - now.pos;
V[now.pos].insert({ j, dst / now.range });
if (vitP[j].find(now.range) != vitP[j].end()) break;
}
for (int j = now.pos - now.range; j >= 0; j -= now.range) {
if (vitP[j].size() == 0) continue;
int dst = now.pos - j;
V[now.pos].insert({ j, dst / now.range });
if (vitP[j].find(now.range) != vitP[j].end()) break;
}
}
priority_queue<Q> pq;
pq.push({ s, 0 });
while (!pq.empty()) {
Q top = pq.top(); pq.pop();
if (top.pos == e) {
cout << top.cost;
return 0;
}
for (pi w : V[top.pos]) {
if (dst[w.to] > top.cost + w.cost) {
dst[w.to] = top.cost + w.cost;
pq.push({ w.to, dst[w.to] });
}
}
}
cout << -1;
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... |