#include <bits/stdc++.h>
using namespace std;
int n,m;
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
assert(n<=2000);
assert(m<=4000);
int a[n];
for(int &i : a){
cin >> i;
assert(i<=2000);
}
vector<int>g[n];
for(int i = 0;i<m;i++){
int a,b;
cin >> a >> b;
a--;b--;
g[a].push_back(b);
g[b].push_back(a);
}
priority_queue<array<int,3>,vector<array<int,3>>,greater<array<int,3>>>pq;
pq.push({0,0,0});
//time,current height, place, downbool
set<array<int,2>>vis;
vis.insert({0,0});
//height,place,downbool
long long ans = 0;
while(!pq.empty()){
array<int,3>cur = pq.top();
//cout << cur[0] << " " << cur[1] << " " << cur[2] << " " << cur[3] << "\n";
pq.pop();
if(cur[1]>2000){
continue;
}
if(cur[1]==0&&cur[2]==n-1){
ans=cur[0];
break;
}
if(vis.find({cur[1]+1,cur[2]})==vis.end())
pq.push({cur[0]+1,cur[1]+1,cur[2]});
if(cur[1]>a[cur[2]]&&vis.find({cur[1]-1,cur[2]})==vis.end()){
pq.push({cur[0]+1,cur[1]-1,cur[2]});
}
for(int i : g[cur[2]]){
if(cur[1]-1>=a[i]&&vis.find({cur[1]-1,i})==vis.end()){
pq.push({cur[0]+1,cur[1]-1,i});
vis.insert({cur[1]-1,i});
}
if(cur[1]>=a[i]&&vis.find({cur[1],i})==vis.end()){
pq.push({cur[0]+1,cur[1],i});
vis.insert({cur[1],i});
}
if(cur[1]+1>=a[i]&&vis.find({cur[1]+1,i})==vis.end()){
pq.push({cur[0]+1,cur[1]+1,i});
vis.insert({cur[1]+1,i});
}
}
}
cout << ans;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Runtime error |
2 ms |
592 KB |
Execution killed with signal 6 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
432 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Execution timed out |
1044 ms |
101028 KB |
Time limit exceeded |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
432 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Execution timed out |
1044 ms |
101028 KB |
Time limit exceeded |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Runtime error |
2 ms |
592 KB |
Execution killed with signal 6 |
3 |
Halted |
0 ms |
0 KB |
- |