#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define endl "\n"
#define all(aa) aa.begin(), aa.end()
int main(){
int n, m;
cin>>n>>m;
vector<int> v(n);
vector<vector<int>> g(n);
for(auto &e:v) cin>>e;
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);
}
vector<int> ans(n, -1), ans2(n, -1), tm(n, -1), tm2(n, -1);
priority_queue<tuple<int, int, int>> pq;
pq.push({0, 0, 0});
while(pq.size()){
auto [t, h, cur]=pq.top();
pq.pop();
if(tm[cur]!=-1) continue;
t=tm[cur]=-t;
h=ans[cur]=-h;
for(auto ch:g[cur]){
pq.push({-max(v[ch], t+1), -max(h, v[ch]), ch});
}
}
pq.push({0, 0, n-1});
while(pq.size()){
auto [t, h, cur]=pq.top();
pq.pop();
if(tm2[cur]!=-1) continue;
h=ans2[cur]=-h;
t=tm2[cur]=-t;
for(auto ch:g[cur]){
pq.push({-max(v[ch], t+1), -max(h, v[ch]), ch});
}
}
int res=INT_MAX;
for(int i=0; i<n; i++){
res=min(res, tm[i]+tm2[i]+abs(ans[i]-ans2[i]));
}
cout<<res<<endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |