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;
struct info{
int badge;
int cur_pos;
int nr_of_jumps;
int type;
};
bool vis[30002];
deque<info> dq;
vector<int> doges[30002];
int jmp[30002];
int n,m;
void bfs01(int doge, int pos, int kon){
dq.push_back({doge,pos,0,1});
dq.push_back({doge,pos,0,-1});
while(dq.size()){
info now=dq.front();
dq.pop_front();
if (now.cur_pos==kon){
cout << now.nr_of_jumps << '\n';
return;
}
for (auto u : doges[now.cur_pos])if (!vis[u]){
vis[u]=true;
if (jmp[u]==jmp[now.badge])continue;
dq.push_front({u,now.cur_pos,now.nr_of_jumps,1});
dq.push_front({u,now.cur_pos,now.nr_of_jumps,-1});
}
if (now.cur_pos>=jmp[now.badge] && now.type==-1){
dq.push_back({now.badge,now.cur_pos-jmp[now.badge],now.nr_of_jumps+1,now.type});
}
if (now.cur_pos+jmp[now.badge]<n && now.type==1){
dq.push_back({now.badge,now.cur_pos+jmp[now.badge],now.nr_of_jumps+1,now.type});
}
}
cout << "-1\n";
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int b,p,st,nd;
cin >> n >> m;
cin >> st >> jmp[0];
doges[st].push_back(0);
cin >> nd >> p;
for (int i = 2; i<m; i++){
cin >> b >> jmp[i];
doges[b].push_back(i);
}
bfs01(0,st,nd);
}
# | 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... |