이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
struct loc{int x; int doge; int d; bool r;};
int N, M;
int b[30000], p[30000];
vector<int> graph[30000];
int dist[30000];
signed main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
cin >> N >> M;
for(int i = 0; i < M; i++){
cin >> b[i] >> p[i];
graph[b[i]].push_back(i);
}
fill(dist, dist+N, -1);
deque<loc> q;
q.push_back({b[0], 0, 0, true});
q.push_back({b[0], 0, 0, false});
while(!q.empty()){
if(~dist[b[1]]) break;
int x = q.front().x, doge = q.front().doge, d = q.front().d;
bool r = q.front().r;
q.pop_front();
if(!(~dist[x])){
dist[x] = d;
for(int i : graph[x]){
if(i != doge){
q.push_front({x, i, d, true});
q.push_front({x, i, d, false});
}
}
}
int dv = p[doge];
if(!r) dv = -dv;
x += dv;
d++;
while(x >= 0 && x < N && ~dist[x]){
x += dv;
d++;
}
if(x < 0 || x >= N) continue;
q.push_back({x, doge, d, r});
}
cout << dist[b[1]] << endl;
}
# | 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... |