This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
const int mxN = 3e4 + 1;
pair<int, int> doge[mxN];
queue<int> jump[mxN];
map<int, int> dist[mxN];
priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>, greater<pair<int, pair<int, int>>>> pq;
int n;
void AddElement(pair<int, pair<int, int>> &nxt) {
if (!(0 <= nxt.second.first && nxt.second.first < n)) return;
if (dist[nxt.second.first].find(nxt.second.second) == dist[nxt.second.first].end()) {
pq.push(nxt);
dist[nxt.second.first][nxt.second.second] = nxt.first;
} else if (dist[nxt.second.first][nxt.second.second] > nxt.first) {
pq.push(nxt);
dist[nxt.second.first][nxt.second.second] = nxt.first;
}
}
void solve() {
int m;
cin >> n >> m;
for (int i = 0; i < m; i++) {
cin >> doge[i].first >> doge[i].second;
if (i != 0) jump[doge[i].first].push(doge[i].second);
}
pq.push({0, doge[0]});
dist[doge[0].first][doge[0].second] = 0;
while (!pq.empty()) {
pair<int, pair<int, int>> cur = pq.top();
pq.pop();
if (dist[cur.second.first][cur.second.second] < cur.first) continue;
if (cur.second.first == doge[1].first) {
cout << cur.first << endl;
return;
}
pair<int, pair<int, int>> nxt;
while (!jump[cur.second.first].empty()) {
int delta = jump[cur.second.first].front();
jump[cur.second.first].pop();
nxt = {cur.first, {cur.second.first, delta}};
AddElement(nxt);
}
nxt = {cur.first + 1, {cur.second.first - cur.second.second, cur.second.second}};
AddElement(nxt);
nxt = {cur.first + 1, {cur.second.first + cur.second.second, cur.second.second}};
AddElement(nxt);
}
cout << "-1\n";
}
int main() {
ios::sync_with_stdio(0); cin.tie(0);
solve();
}
# | 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... |