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;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define ff first
#define ss second
ll ttt;
const ll INF=1e18;
const ll MOD=1e9+7;
const ll N=5000007;
ll n,m,k;
ll si,sj,ei,ej;
ll len[N];
set<int>jjj;
vector<int>jj;
unordered_map<int,int>d;
vector<pair<int,ll>>g[N];
int vertexNumber(int i, int j){
return j*n+i;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
// freopen("Cinput.txt","r",stdin);
// freopen("Coutput.txt","w",stdout);
cin>>n>>si>>sj>>ei>>ej;
si--,sj--,ei--,ej--;
jjj.insert(0);
jjj.insert(sj);
jjj.insert(ej);
for(int i=0;i<n;i++){
cin>>len[i];
len[i]++;
jjj.insert(len[i]-1);
}
int cnt=0;
for(auto x:jjj){
jj.push_back(x);
d[x]=cnt;
cnt++;
}
for(int i=0;i<n;i++){
for(int j=0;j<jj.size();j++){
int v=vertexNumber(i,j);
if(jj[j]>=len[i])break;
// RIGHT
if(j!=jj.size()-1&&jj[j+1]<len[i]){
g[v].push_back({vertexNumber(i,j+1),jj[j+1]-jj[j]});
}
else if(i!=n-1){
g[v].push_back({vertexNumber(i+1,0),1});
}
// LEFT
if(j!=0){
g[v].push_back({vertexNumber(i,j-1),jj[j]-jj[j-1]});
}
else if(i!=0){
g[v].push_back({vertexNumber(i-1,d[len[i-1]-1]),1});
}
// UP
if(i!=0&&jj[j]<len[i-1]){
g[v].push_back({vertexNumber(i-1,j),1});
}
else if(i!=0){
g[v].push_back({vertexNumber(i-1,d[len[i-1]-1]),1});
}
// DOWN
if(i!=n-1&&jj[j]<len[i+1]){
g[v].push_back({vertexNumber(i+1,j),1});
}
else if(i!=n-1){
g[v].push_back({vertexNumber(i+1,d[len[i+1]-1]),1});
}
}
}
// for(auto x:jj){
// cout<<x<<" ";
// }
// cout<<endl;
// for(int i=0;i<N;i++){
// if(!g[i].empty()){
// cout<<i<<": ";
// for(auto e:g[i]){
// cout<<"{"<<e.ff<<", "<<e.ss<<"}, ";
// }
// cout<<endl;
// }
// }
priority_queue<pair<ll,int>>q;
vector<ll>dist(N, INF);
int start=vertexNumber(si,d[sj]);
// cout<<start<<endl;
q.push({0,start});
dist[start]=0;
// cout<<"OK"<<endl;
while(!q.empty()){
int v=q.top().ss;
ll w=-q.top().ff;
// cout<<v<<" "<<w<<endl;
q.pop();
if(dist[v]==w){
for(auto e:g[v]){
int to=e.ff;
ll d=e.ss;
if(dist[to]>dist[v]+d){
dist[to]=dist[v]+d;
// cout<<to<<" "<<dist[to]<<endl;
q.push({-dist[to],to});
}
}
}
}
// cout<<vertexNumber(ei,d[ej])<<endl;
cout<<dist[vertexNumber(ei,d[ej])]<<endl;
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:51:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
51 | for(int j=0;j<jj.size();j++){
| ~^~~~~~~~~~
Main.cpp:56:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
56 | if(j!=jj.size()-1&&jj[j+1]<len[i]){
| ~^~~~~~~~~~~~~
# | 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... |