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;
const int nx=2e5+5;
int n, m, a[nx], u, v, vl[nx], rvl[nx], l[nx], rl[nx], vs[nx];
vector<int> d[nx];
pair<int, int> res={1e9, 0};
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
int main()
{
cin.tie(NULL)->sync_with_stdio(false);
cin>>n>>m;
for (int i=1; i<=n; i++) cin>>a[i], vl[i]=rvl[i]=l[i]=rl[i]=1e9;
for (int i=1; i<=m; i++) cin>>u>>v, d[u].push_back(v), d[v].push_back(u);
vl[1]=rvl[n]=l[1]=rl[n]=0;
pq.push({0, 1});
while (!pq.empty())
{
auto [cw, u]=pq.top();
pq.pop();
if (vs[u]) continue;
vs[u]=1;
for (auto v:d[u]) if (vl[v]>max(cw+1, a[v])) l[v]=l[u]>a[v]?l[u]-1:a[v], vl[v]=max(cw+1, a[v]), pq.push({vl[v], v});
}
pq.push({0, n});
for (int i=1; i<=n; i++) vs[i]=0;
while (!pq.empty())
{
auto [cw, u]=pq.top();
pq.pop();
if (vs[u]) continue;
vs[u]=1;
for (auto v:d[u]) if (rvl[v]>max(cw+1, a[v])) rl[v]=rl[u]>a[v]?rl[u]-1:a[v], rvl[v]=max(cw+1, a[v]), pq.push({rvl[v], v});
}
/*
pq.push({0, 1});
for (int i=1; i<=n; i++) vs[i]=0;
while (!pq.empty())
{
auto [cw, u]=pq.top();
pq.pop();
if (vs[u]) continue;
vs[u]=1;
for (auto v:d[u])
{
int cst=l[u]>a[v]?l[u]-1:a[v];
if (l[v]>cst) l[v]=cst, pq.push({l[v], v});
}
}
pq.push({0, n});
for (int i=1; i<=n; i++) vs[i]=0;
while (!pq.empty())
{
auto [cw, u]=pq.top();
pq.pop();
if (vs[u]) continue;
vs[u]=1;
for (auto v:d[u])
{
int cst=rl[u]>a[v]?rl[u]-1:a[v];
if (rl[v]>cst) rl[v]=cst, pq.push({rl[v], v});
}
}*/
for (int i=1; i<=n; i++)
{
if (vl[i]<rl[i]) res=min(res, {vl[i]+rvl[i]+rl[i]-vl[i], i});
else if (rvl[i]<l[i]) res=min(res, {vl[i]+rvl[i]+l[i]-rvl[i], i});
else res=min(res, {vl[i]+rvl[i], i});
}
/*
cout<<"vl ";
for (int i=1; i<=n; i++) cout<<vl[i]<<' ';
cout<<'\n';
cout<<"rvl ";
for (int i=1; i<=n; i++) cout<<rvl[i]<<' ';
cout<<'\n';
cout<<"l ";
for (int i=1; i<=n; i++) cout<<l[i]<<' ';
cout<<'\n';
cout<<"rl ";
for (int i=1; i<=n; i++) cout<<rl[i]<<' ';
cout<<'\n';
*/
cout<<res.first;
}
# | 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... |