#include <bits/stdc++.h>
using namespace std;
#define fileio() freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout)
#define fastio() cin.tie(0), ios_base::sync_with_stdio(0)
#define sp " "
#define endl "\n"
#define pii pair<int, int>
#define st first
#define nd second
#define pb push_back
#define N 300005
#define int long long
const int INF = 2e18 + 7;
pii d[2][N];
vector<int> adj[N];
int arr[N], maks[2][N];
int32_t main(){
//fileio();
fastio();
int n, m;
cin>>n>>m;
for (int i = 1; i <= n; i++){
cin>>arr[i];
}
for (int i = 1; i <= m; i++){
int u, v;
cin>>u>>v;
adj[u].pb(v);
adj[v].pb(u);
}
int root = 1;
for (int i = 0; i < 2; i++){
priority_queue<array<int, 3>> q;
q.push({0, arr[root], root});
for (int j = 1; j <= n; j++){
d[i][j] = {INF, INF};
maks[i][j] = INF;
}
d[i][root] = {0, arr[root]};
while(!q.empty()){
int top = q.top()[2];
int dd = -q.top()[0];
int m = q.top()[1];
q.pop();
for (auto j : adj[top]){
pii dist = {max(arr[j], dd + 1), -max(m, arr[j])};
if (d[i][j] > dist){
d[i][j] = dist;
q.push({-dist.st, -dist.nd, j});
}
if (m <= arr[j] && maks[i][j] > dist.st){
maks[i][j] = dist.st;
q.push({-dist.st, arr[j], j});
}
}
}
root = n;
}
int ans = INF;
for (int i = 1; i <= n; i++){
ans = min(ans, maks[0][i] + maks[1][i]);
}
cout<<ans<<endl;
cerr<<"time taken : "<<(float)clock() / CLOCKS_PER_SEC<<" seconds\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
14684 KB |
Output is correct |
2 |
Correct |
69 ms |
29448 KB |
Output is correct |
3 |
Correct |
61 ms |
29448 KB |
Output is correct |
4 |
Correct |
61 ms |
29448 KB |
Output is correct |
5 |
Correct |
61 ms |
29264 KB |
Output is correct |
6 |
Correct |
62 ms |
29268 KB |
Output is correct |
7 |
Correct |
64 ms |
29488 KB |
Output is correct |
8 |
Incorrect |
2 ms |
14684 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
14684 KB |
Output is correct |
2 |
Correct |
3 ms |
14684 KB |
Output is correct |
3 |
Incorrect |
2 ms |
14684 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
14684 KB |
Output is correct |
2 |
Correct |
3 ms |
14684 KB |
Output is correct |
3 |
Incorrect |
2 ms |
14684 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
14684 KB |
Output is correct |
2 |
Correct |
69 ms |
29448 KB |
Output is correct |
3 |
Correct |
61 ms |
29448 KB |
Output is correct |
4 |
Correct |
61 ms |
29448 KB |
Output is correct |
5 |
Correct |
61 ms |
29264 KB |
Output is correct |
6 |
Correct |
62 ms |
29268 KB |
Output is correct |
7 |
Correct |
64 ms |
29488 KB |
Output is correct |
8 |
Incorrect |
2 ms |
14684 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |