#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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8784 KB |
Output is correct |
2 |
Correct |
47 ms |
15964 KB |
Output is correct |
3 |
Correct |
50 ms |
19016 KB |
Output is correct |
4 |
Correct |
52 ms |
19016 KB |
Output is correct |
5 |
Correct |
53 ms |
19544 KB |
Output is correct |
6 |
Correct |
55 ms |
19672 KB |
Output is correct |
7 |
Correct |
54 ms |
19528 KB |
Output is correct |
8 |
Correct |
2 ms |
8784 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8784 KB |
Output is correct |
2 |
Correct |
2 ms |
8784 KB |
Output is correct |
3 |
Correct |
2 ms |
8848 KB |
Output is correct |
4 |
Correct |
2 ms |
8952 KB |
Output is correct |
5 |
Correct |
2 ms |
8784 KB |
Output is correct |
6 |
Correct |
2 ms |
8784 KB |
Output is correct |
7 |
Correct |
2 ms |
8868 KB |
Output is correct |
8 |
Correct |
2 ms |
8924 KB |
Output is correct |
9 |
Correct |
2 ms |
8952 KB |
Output is correct |
10 |
Correct |
3 ms |
8784 KB |
Output is correct |
11 |
Correct |
2 ms |
8952 KB |
Output is correct |
12 |
Correct |
3 ms |
8784 KB |
Output is correct |
13 |
Correct |
2 ms |
8784 KB |
Output is correct |
14 |
Correct |
3 ms |
8784 KB |
Output is correct |
15 |
Correct |
3 ms |
8784 KB |
Output is correct |
16 |
Correct |
3 ms |
8784 KB |
Output is correct |
17 |
Correct |
3 ms |
8784 KB |
Output is correct |
18 |
Correct |
2 ms |
8952 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8784 KB |
Output is correct |
2 |
Correct |
2 ms |
8784 KB |
Output is correct |
3 |
Correct |
2 ms |
8848 KB |
Output is correct |
4 |
Correct |
2 ms |
8952 KB |
Output is correct |
5 |
Correct |
2 ms |
8784 KB |
Output is correct |
6 |
Correct |
2 ms |
8784 KB |
Output is correct |
7 |
Correct |
2 ms |
8868 KB |
Output is correct |
8 |
Correct |
2 ms |
8924 KB |
Output is correct |
9 |
Correct |
2 ms |
8952 KB |
Output is correct |
10 |
Correct |
3 ms |
8784 KB |
Output is correct |
11 |
Correct |
2 ms |
8952 KB |
Output is correct |
12 |
Correct |
3 ms |
8784 KB |
Output is correct |
13 |
Correct |
2 ms |
8784 KB |
Output is correct |
14 |
Correct |
3 ms |
8784 KB |
Output is correct |
15 |
Correct |
3 ms |
8784 KB |
Output is correct |
16 |
Correct |
3 ms |
8784 KB |
Output is correct |
17 |
Correct |
3 ms |
8784 KB |
Output is correct |
18 |
Correct |
2 ms |
8952 KB |
Output is correct |
19 |
Correct |
3 ms |
8784 KB |
Output is correct |
20 |
Correct |
2 ms |
8784 KB |
Output is correct |
21 |
Correct |
3 ms |
8860 KB |
Output is correct |
22 |
Correct |
3 ms |
8784 KB |
Output is correct |
23 |
Correct |
3 ms |
9080 KB |
Output is correct |
24 |
Correct |
3 ms |
8796 KB |
Output is correct |
25 |
Correct |
3 ms |
8784 KB |
Output is correct |
26 |
Correct |
3 ms |
8784 KB |
Output is correct |
27 |
Correct |
3 ms |
8784 KB |
Output is correct |
28 |
Correct |
3 ms |
8856 KB |
Output is correct |
29 |
Correct |
3 ms |
8856 KB |
Output is correct |
30 |
Correct |
3 ms |
8784 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8784 KB |
Output is correct |
2 |
Correct |
47 ms |
15964 KB |
Output is correct |
3 |
Correct |
50 ms |
19016 KB |
Output is correct |
4 |
Correct |
52 ms |
19016 KB |
Output is correct |
5 |
Correct |
53 ms |
19544 KB |
Output is correct |
6 |
Correct |
55 ms |
19672 KB |
Output is correct |
7 |
Correct |
54 ms |
19528 KB |
Output is correct |
8 |
Correct |
2 ms |
8784 KB |
Output is correct |
9 |
Correct |
2 ms |
8784 KB |
Output is correct |
10 |
Correct |
2 ms |
8784 KB |
Output is correct |
11 |
Correct |
2 ms |
8848 KB |
Output is correct |
12 |
Correct |
2 ms |
8952 KB |
Output is correct |
13 |
Correct |
2 ms |
8784 KB |
Output is correct |
14 |
Correct |
2 ms |
8784 KB |
Output is correct |
15 |
Correct |
2 ms |
8868 KB |
Output is correct |
16 |
Correct |
2 ms |
8924 KB |
Output is correct |
17 |
Correct |
2 ms |
8952 KB |
Output is correct |
18 |
Correct |
3 ms |
8784 KB |
Output is correct |
19 |
Correct |
2 ms |
8952 KB |
Output is correct |
20 |
Correct |
3 ms |
8784 KB |
Output is correct |
21 |
Correct |
2 ms |
8784 KB |
Output is correct |
22 |
Correct |
3 ms |
8784 KB |
Output is correct |
23 |
Correct |
3 ms |
8784 KB |
Output is correct |
24 |
Correct |
3 ms |
8784 KB |
Output is correct |
25 |
Correct |
3 ms |
8784 KB |
Output is correct |
26 |
Correct |
2 ms |
8952 KB |
Output is correct |
27 |
Correct |
3 ms |
8784 KB |
Output is correct |
28 |
Correct |
2 ms |
8784 KB |
Output is correct |
29 |
Correct |
3 ms |
8860 KB |
Output is correct |
30 |
Correct |
3 ms |
8784 KB |
Output is correct |
31 |
Correct |
3 ms |
9080 KB |
Output is correct |
32 |
Correct |
3 ms |
8796 KB |
Output is correct |
33 |
Correct |
3 ms |
8784 KB |
Output is correct |
34 |
Correct |
3 ms |
8784 KB |
Output is correct |
35 |
Correct |
3 ms |
8784 KB |
Output is correct |
36 |
Correct |
3 ms |
8856 KB |
Output is correct |
37 |
Correct |
3 ms |
8856 KB |
Output is correct |
38 |
Correct |
3 ms |
8784 KB |
Output is correct |
39 |
Correct |
109 ms |
20132 KB |
Output is correct |
40 |
Correct |
80 ms |
19148 KB |
Output is correct |
41 |
Correct |
79 ms |
19272 KB |
Output is correct |
42 |
Correct |
73 ms |
19388 KB |
Output is correct |
43 |
Correct |
73 ms |
19216 KB |
Output is correct |
44 |
Correct |
83 ms |
19616 KB |
Output is correct |
45 |
Correct |
85 ms |
19500 KB |
Output is correct |
46 |
Correct |
77 ms |
19180 KB |
Output is correct |
47 |
Correct |
75 ms |
19536 KB |
Output is correct |
48 |
Correct |
74 ms |
19528 KB |
Output is correct |
49 |
Correct |
97 ms |
20040 KB |
Output is correct |
50 |
Correct |
98 ms |
20044 KB |
Output is correct |
51 |
Correct |
86 ms |
19792 KB |
Output is correct |
52 |
Correct |
149 ms |
22220 KB |
Output is correct |
53 |
Correct |
130 ms |
21320 KB |
Output is correct |
54 |
Correct |
128 ms |
21444 KB |
Output is correct |
55 |
Correct |
177 ms |
21708 KB |
Output is correct |
56 |
Correct |
187 ms |
24552 KB |
Output is correct |
57 |
Correct |
202 ms |
24512 KB |
Output is correct |
58 |
Correct |
190 ms |
25412 KB |
Output is correct |
59 |
Correct |
178 ms |
25600 KB |
Output is correct |
60 |
Correct |
175 ms |
25824 KB |
Output is correct |
61 |
Correct |
175 ms |
25060 KB |
Output is correct |
62 |
Correct |
165 ms |
24512 KB |
Output is correct |
63 |
Correct |
152 ms |
24516 KB |
Output is correct |
64 |
Correct |
153 ms |
24512 KB |
Output is correct |
65 |
Correct |
167 ms |
24508 KB |
Output is correct |
66 |
Correct |
198 ms |
24668 KB |
Output is correct |
67 |
Correct |
202 ms |
24592 KB |
Output is correct |