#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>>> 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;
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 (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() {
solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
21900 KB |
Output is correct |
2 |
Correct |
15 ms |
21896 KB |
Output is correct |
3 |
Correct |
15 ms |
21880 KB |
Output is correct |
4 |
Correct |
13 ms |
21844 KB |
Output is correct |
5 |
Correct |
16 ms |
21836 KB |
Output is correct |
6 |
Correct |
13 ms |
21908 KB |
Output is correct |
7 |
Correct |
13 ms |
21820 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
21844 KB |
Output is correct |
2 |
Correct |
13 ms |
21912 KB |
Output is correct |
3 |
Correct |
13 ms |
21908 KB |
Output is correct |
4 |
Correct |
13 ms |
21916 KB |
Output is correct |
5 |
Correct |
13 ms |
21844 KB |
Output is correct |
6 |
Correct |
16 ms |
21980 KB |
Output is correct |
7 |
Correct |
13 ms |
21844 KB |
Output is correct |
8 |
Correct |
12 ms |
21908 KB |
Output is correct |
9 |
Correct |
13 ms |
21916 KB |
Output is correct |
10 |
Incorrect |
17 ms |
21900 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
21940 KB |
Output is correct |
2 |
Correct |
16 ms |
21892 KB |
Output is correct |
3 |
Correct |
14 ms |
21844 KB |
Output is correct |
4 |
Correct |
13 ms |
21844 KB |
Output is correct |
5 |
Correct |
17 ms |
21912 KB |
Output is correct |
6 |
Correct |
14 ms |
21844 KB |
Output is correct |
7 |
Correct |
13 ms |
21912 KB |
Output is correct |
8 |
Correct |
13 ms |
21896 KB |
Output is correct |
9 |
Correct |
14 ms |
21844 KB |
Output is correct |
10 |
Incorrect |
15 ms |
21856 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
21852 KB |
Output is correct |
2 |
Correct |
13 ms |
21844 KB |
Output is correct |
3 |
Correct |
15 ms |
21908 KB |
Output is correct |
4 |
Correct |
13 ms |
21844 KB |
Output is correct |
5 |
Correct |
14 ms |
21804 KB |
Output is correct |
6 |
Correct |
13 ms |
21904 KB |
Output is correct |
7 |
Correct |
17 ms |
21908 KB |
Output is correct |
8 |
Correct |
14 ms |
21796 KB |
Output is correct |
9 |
Correct |
14 ms |
21972 KB |
Output is correct |
10 |
Incorrect |
13 ms |
21908 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
21844 KB |
Output is correct |
2 |
Correct |
13 ms |
21908 KB |
Output is correct |
3 |
Correct |
13 ms |
21824 KB |
Output is correct |
4 |
Correct |
13 ms |
21844 KB |
Output is correct |
5 |
Correct |
13 ms |
21844 KB |
Output is correct |
6 |
Correct |
13 ms |
21844 KB |
Output is correct |
7 |
Correct |
13 ms |
21860 KB |
Output is correct |
8 |
Correct |
12 ms |
21844 KB |
Output is correct |
9 |
Correct |
13 ms |
21844 KB |
Output is correct |
10 |
Incorrect |
13 ms |
21896 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |