이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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... |