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 MAX_N = 30000;
int B[MAX_N], P[MAX_N];
map <int, int> dist[MAX_N];
vector <int> graph[MAX_N];
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int N, M;
cin >> N >> M;
for(int i = 0; i < M; i++) {
cin >> B[i] >> P[i];
graph[B[i]].push_back(P[i]);
}
for(int i = 0; i < N; i++) {
sort(graph[i].begin(), graph[i].end());
graph[i].resize(unique(graph[i].begin(), graph[i].end()) - graph[i].begin());
}
queue <pair <int, int>> q;
q.emplace(B[0], P[0]);
dist[B[0]][P[0]] = 0;
while(!q.empty()) {
auto [b, p] = q.front();
q.pop();
if(b == B[1]) {
cout << dist[b][p];
return 0;
}
if(b - p >= 0 and !dist[b - p].count(p)) {
dist[b - p][p] = dist[b][p] + 1;
q.emplace(b - p, p);
}
if(b + p < N and !dist[b + p].count(p)) {
dist[b + p][p] = dist[b][p] + 1;
q.emplace(b + p, p);
}
for(auto p2 : graph[b]) {
if(b - p2 >= 0 and !dist[b - p2].count(p2)) {
dist[b - p2][p2] = dist[b][p] + 1;
q.emplace(b - p2, p2);
}
if(b + p2 < N and !dist[b + p2].count(p2)) {
dist[b + p2][p2] = dist[b][p] + 1;
q.emplace(b + p2, p2);
}
}
}
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... |