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 all(fl) fl.begin(),fl.end()
#define pb push_back
#define fi first
#define se second
#define for1(i,j,k) for(int i=j;i<=k;i++)
#define for2(i,j,k) for(int i=j;i>=k;i--)
#define for3(i,j,k,l) for(int i=j;i<=k;i+=l)
#define lb lower_bound
#define ub upper_bound
#define sz(a) (int)a.size()
#define pii pair<int,int>
#define pli pair<long long,int>
#define gcd __gcd
#define lcm(x,y) x*y/__gcd(x,y)
#define lastbit(i) __builtin_ctz(i)
#define pil pair<int,long long>
const int maxn=2e5+9;
const long long inf=1e15;
long long b[maxn];
vector<int>a[maxn];
int n;
struct cus{
int u;
long long val;
bool operator < (const cus &p)const {
return val>p.val;
}
};
vector<long long> cal(int s){
vector<long long>f(n+1);
vector<bool>vis(n+1);
for1(i,1,n)f[i]=inf;
f[s]=0;
priority_queue<cus>t;
t.push({s,0});
while (!t.empty()){
auto u=t.top();
t.pop();
if (vis[u.u])continue;
vis[u.u]=1;
for (auto v:a[u.u]){
if (f[v]>max(b[v],f[u.u]+1)){
f[v]=max(b[v],f[u.u]+1);
t.push({v,f[v]});
}
}
}
return f;
}
long long ans[maxn];
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
//freopen("temp.INP","r",stdin);
//freopen("temp.OUT","w",stdout);
int m;
cin>>n>>m;
for1(i,1,n)cin>>b[i];
for1(i,1,m){
int u,v;
cin>>u>>v;
a[u].pb(v);
a[v].pb(u);
}
vector<long long>tmp=cal(1);
for1(i,1,n){
ans[i]+=tmp[i];
}
tmp=cal(n);
long long vcl=inf;
for1(i,1,n){
if (abs(ans[i]-tmp[i])<=1)vcl=min(vcl,ans[i]+tmp[i]);
}
cout<<vcl;
}
# | 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... |