이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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];
queue<pair<int, int>> q[2];
int curdist = 0;
int n;
void PushElement(pair<int, pair<int, int>> &nxt) {
q[(nxt.first & 1)].push(nxt.second);
}
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()) {
PushElement(nxt);
dist[nxt.second.first][nxt.second.second] = nxt.first;
} else if (dist[nxt.second.first][nxt.second.second] > nxt.first) {
PushElement(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);
}
pair<int, pair<int, int>> nxt;
nxt = {0, doge[0]};
AddElement(nxt);
while (!q[(curdist & 1)].empty()) {
while (!q[(curdist & 1)].empty()) {
pair<int, int> cur = q[(curdist & 1)].front();
q[(curdist & 1)].pop();
if (dist[cur.first][cur.second] < curdist) continue;
if (cur.first == doge[1].first) {
cout << curdist << endl;
return;
}
while (!jump[cur.first].empty()) {
int delta = jump[cur.first].front();
jump[cur.first].pop();
nxt = {curdist, {cur.first, delta}};
AddElement(nxt);
}
nxt = {curdist + 1, {cur.first - cur.second, cur.second}};
AddElement(nxt);
nxt = {curdist + 1, {cur.first + cur.second, cur.second}};
AddElement(nxt);
}
curdist++;
}
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... |