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;
#define int long long
#define pii pair<int, int>
int N;
vector<int> l;
vector<pii> interesting;
vector<pair<int, pii>> get_adj(pii pos){
int line = pos.first;
int col = pos.second;
vector<pair<int, pii>> res;
for(auto e: interesting){
if(e.first == line){
res.push_back({abs(e.second-col), e});
}
}
res.push_back({col, {line, 0}});
res.push_back({l[line]-1-col, {line, l[line]-1}});
if(line<N-1){
res.push_back({1, {line+1, min(l[line+1]-1, col)}});
if(col == l[line]-1){
res.push_back({1, {line+1,0}});
}
}
if(line>0){
res.push_back({1, {line-1, min(l[line-1]-1, col)}});
if(col == 0){
res.push_back({1, {line-1, l[line-1]-1}});
}
}
return res;
}
int flat(pii a){
return a.first* 1001 + a.second;
}
int BFS(pii begin, pii dest){
priority_queue<pair<int, pii>> pq;
pq.push({-0, begin});
unordered_map<int, int> dist;
dist[flat(begin)] = 0;
while(pq.size()>0){
auto cur= pq.top();
cur.first*=-1;
pq.pop();
//cout<<cur.first<<" "<<cur.second.first<<" "<<cur.second.second<<endl;
for(auto e: get_adj(cur.second)){
int cost = e.first + cur.first;
if(dist.find(flat(e.second)) == dist.end() || dist[flat(e.second)] >cost){
pq.push({-cost, e.second});
dist[flat(e.second)] = cost;
}
}
}
return dist[flat(dest)];
}
signed main(){
cin>>N;
l.resize(N);
pii begin;
pii dest;
cin>>begin.first>>begin.second;
cin>>dest.first>>dest.second;
begin.first--;
begin.second--;
dest.first--;
dest.second--;
interesting.push_back(begin);
interesting.push_back(dest);
for(int i = 0; i<N; i++){
cin>>l[i];
l[i]++;
}
cout<<BFS(begin, dest)<<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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |