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 ll long long
#define int ll
#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define REP1(i, n) FOR(i, 1, n+1)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
#define RREP1(i, n) for(int i = (n); i >= 1; i--)
#define pii pair<int, int>
#define ppp pair<pii, pii>
#define ppi pair<pii, int>
#define pip pair<int, pii>
#define f first
#define s second
#define pb push_back
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) (int)((x).size())
#define endl '\n'
#define IOS() ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
const ll maxn = 2e5+5;
const ll maxm = 1e4+5;
const ll mod = 1e9+7;
const ll inf = 1ll<<60;
vector<int> g[maxn];
vector<int> h(maxn);
int n, m;
int dis[maxn][2];
vector<bool> occ(maxn);
void dij(int st, bool typ){
// source st, typ = 0: it is n
fill(ALL(occ), 0);
REP1(i, n) dis[i][typ] = inf;
priority_queue<pii, vector<pii>, greater<pii> > pq;
dis[st][typ] = 0;
pq.push({0, st});
while(SZ(pq)){
pii tp = pq.top(); pq.pop();
int u = tp.s;
if (occ[u]) continue;
occ[u] = 1;
for(auto v:g[u]){
if (dis[v][typ] > max(h[v], dis[u][typ]+1)){
dis[v][typ] = max(h[v], dis[u][typ]+1);
pq.push({dis[v][typ], v});
}
}
}
}
signed main(){
IOS();
cin>>n>>m;
REP1(i, n) cin>>h[i];
vector<pii> ee;
REP(i, m){
int u, v; cin>>u>>v;
g[u].pb(v);
g[v].pb(u);
ee.pb({u, v});
}
dij(1, 0); dij(n, 1);
int ans = inf;
REP1(i, n){
ans = min(ans, max(dis[i][0], dis[i][1])*2);
}
for (auto e:ee){
int u = e.f, v = e.s;
ans = min({ans, max(dis[u][0], dis[v][1])*2+1, max(dis[v][0], dis[u][1])*2+1});
}
cout<<ans<<endl;
}
# | 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... |