이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <vector>
#include <queue>
#include <set>
#include <algorithm>
using namespace std;
#define MAXN 30005
#define INF 1000000009
#define pii pair<int, int>
#define mp make_pair
#define f first
#define s second
int n, m;
int b[MAXN];
int p[MAXN];
int dist[MAXN];
vector<int> jumps[MAXN];
void solve(int s) {
dist[b[0]] = 0;
priority_queue<pii, vector<pii>, greater<pii>> pq;
pq.push(mp(0,b[0])); // dist, pos
while (!pq.empty()) {
pii node = pq.top();
pq.pop();
if (node.f > dist[node.s]) {
continue;
}
for(auto jump : jumps[node.s]) {
for (int j = 1; node.s + jump * j < n; ++j) { // forward
if (dist[node.s + jump * j] > node.f + j) {
pq.push(mp(dist[node.s + jump * j] = node.f + j, node.s + jump * j));
if (binary_search(jumps[node.s + jump * j].begin(), jumps[node.s + jump * j].end(), jump)) { // no need to have multiple doges going same "route"
break;
}
}
}
for (int j = 1;node.s - jump * j >= 0;++j) { // backward
if (dist[node.s - jump * j] > node.f + j) {
pq.push(mp(dist[node.s - jump * j] = node.f + j, node.s - jump * j));
if (binary_search(jumps[node.s - jump * j].begin(), jumps[node.s - jump * j].end(), jump)) {
break;
}
}
}
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m;
for(int i = 0; i < m; ++i) {
cin >> b[i] >> p[i];
jumps[b[i]].push_back(p[i]);
}
for(int i = 0; i < n; ++i) {
sort(jumps[i].begin(), jumps[i].end());
jumps[i].erase(unique(jumps[i].begin(), jumps[i].end()), jumps[i].end());
}
for(int i = 0; i < MAXN; ++i) {
dist[i] = INF;
}
solve(b[0]);
if (dist[b[1]] == INF) {
cout << "-1" << endl;
return 0;
}
cout << dist[b[1]] << endl;
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... |