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;
typedef pair<int, int> pii;
#define ff first
#define ss second
#define MP make_pair
const int mxn = 2e5 + 10;
vector<int> adj[mxn];
int a[mxn], n, m;
int dis[2][mxn];
int alt[2][mxn];
int main() {
cin >> n >> m;
for(int i = 1; i <= n; i++)
cin >> a[i];
for(int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
memset(dis[0], -1, sizeof dis[0]);
memset(dis[1], -1, sizeof dis[1]);
priority_queue<pair<pii, int>> pq;
pq.push(MP(MP(0, 0), 1));
while(pq.size()) {
int d = -pq.top().ff.ff;
int mx = pq.top().ff.ss;
int u = pq.top().ss;
pq.pop();
if(dis[0][u] > -1) continue;
dis[0][u] = d;
alt[0][u] = mx;
for(int v : adj[u]) {
if(dis[0][v] > -1) continue;
if(mx >= a[v]) pq.push(MP(MP(-(d + 1), mx), v));
else pq.push(MP(MP(-(d + a[v] - mx), a[v]), v));
}
}
pq.push(MP(MP(0, 0), n));
while(pq.size()) {
int d = -pq.top().ff.ff;
int mx = pq.top().ff.ss;
int u = pq.top().ss;
pq.pop();
if(dis[1][u] > -1) continue;
dis[1][u] = d;
alt[1][u] = mx;
for(int v : adj[u]) {
if(dis[1][v] > -1) continue;
if(mx >= a[v]) pq.push(MP(MP(-(d + 1), mx), v));
else pq.push(MP(MP(-(d + a[v] - mx), a[v]), v));
}
}
int ans = INT_MAX;
for(int i = 1; i <= n; i++)
if(alt[0][i] == alt[1][i])
ans = min(ans, dis[0][i] + dis[1][i]);
cout << ans << endl;
return 0;
}
# | 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... |