#include<bits/stdc++.h>
using namespace std;
#define all(fl) fl.begin(),fl.end()
#define pb push_back
#define fi first
#define se second
#define for1(i,j,k) for(int i=j;i<=k;i++)
#define for2(i,j,k) for(int i=j;i>=k;i--)
#define for3(i,j,k,l) for(int i=j;i<=k;i+=l)
#define lb lower_bound
#define ub upper_bound
#define sz(a) (int)a.size()
#define pii pair<int,int>
#define pli pair<long long,int>
#define gcd __gcd
#define lcm(x,y) x*y/__gcd(x,y)
#define lastbit(i) __builtin_ctz(i)
#define pil pair<int,long long>
const int maxn=2e5+9;
const long long inf=1e15;
long long b[maxn];
vector<int>a[maxn];
int n;
struct cus{
int u;
long long val;
bool operator < (const cus &p)const {
return val>p.val;
}
};
vector<long long> cal(int s){
vector<long long>f(n+1);
vector<bool>vis(n+1);
for1(i,1,n)f[i]=inf;
f[s]=0;
priority_queue<cus>t;
t.push({s,0});
while (!t.empty()){
auto u=t.top();
t.pop();
if (vis[u.u])continue;
vis[u.u]=1;
for (auto v:a[u.u]){
if (f[v]>max(b[v],f[u.u]+1)){
f[v]=max(b[v],f[u.u]+1);
t.push({v,f[v]});
}
}
}
return f;
}
long long ans[maxn];
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
//freopen("temp.INP","r",stdin);
//freopen("temp.OUT","w",stdout);
int m;
cin>>n>>m;
for1(i,1,n)cin>>b[i];
for1(i,1,m){
int u,v;
cin>>u>>v;
a[u].pb(v);
a[v].pb(u);
}
vector<long long>tmp=cal(1);
for1(i,1,n){
ans[i]+=tmp[i];
}
tmp=cal(n);
long long vcl=inf;
for1(i,1,n){
if (abs(ans[i]-tmp[i])<=1)vcl=min(vcl,ans[i]+tmp[i]);
}
cout<<vcl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
59 ms |
17688 KB |
Output is correct |
3 |
Correct |
85 ms |
17476 KB |
Output is correct |
4 |
Correct |
60 ms |
17492 KB |
Output is correct |
5 |
Correct |
78 ms |
17492 KB |
Output is correct |
6 |
Correct |
61 ms |
17556 KB |
Output is correct |
7 |
Correct |
61 ms |
17632 KB |
Output is correct |
8 |
Correct |
1 ms |
6488 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6488 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6488 KB |
Output is correct |
6 |
Correct |
2 ms |
6492 KB |
Output is correct |
7 |
Correct |
2 ms |
6492 KB |
Output is correct |
8 |
Correct |
3 ms |
6492 KB |
Output is correct |
9 |
Correct |
2 ms |
6492 KB |
Output is correct |
10 |
Correct |
2 ms |
6492 KB |
Output is correct |
11 |
Correct |
2 ms |
6504 KB |
Output is correct |
12 |
Correct |
2 ms |
6492 KB |
Output is correct |
13 |
Correct |
2 ms |
6492 KB |
Output is correct |
14 |
Correct |
2 ms |
6748 KB |
Output is correct |
15 |
Correct |
2 ms |
6748 KB |
Output is correct |
16 |
Correct |
2 ms |
6756 KB |
Output is correct |
17 |
Correct |
2 ms |
6748 KB |
Output is correct |
18 |
Correct |
2 ms |
6744 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6488 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6488 KB |
Output is correct |
6 |
Correct |
2 ms |
6492 KB |
Output is correct |
7 |
Correct |
2 ms |
6492 KB |
Output is correct |
8 |
Correct |
3 ms |
6492 KB |
Output is correct |
9 |
Correct |
2 ms |
6492 KB |
Output is correct |
10 |
Correct |
2 ms |
6492 KB |
Output is correct |
11 |
Correct |
2 ms |
6504 KB |
Output is correct |
12 |
Correct |
2 ms |
6492 KB |
Output is correct |
13 |
Correct |
2 ms |
6492 KB |
Output is correct |
14 |
Correct |
2 ms |
6748 KB |
Output is correct |
15 |
Correct |
2 ms |
6748 KB |
Output is correct |
16 |
Correct |
2 ms |
6756 KB |
Output is correct |
17 |
Correct |
2 ms |
6748 KB |
Output is correct |
18 |
Correct |
2 ms |
6744 KB |
Output is correct |
19 |
Correct |
2 ms |
6492 KB |
Output is correct |
20 |
Correct |
2 ms |
6492 KB |
Output is correct |
21 |
Correct |
2 ms |
6588 KB |
Output is correct |
22 |
Correct |
2 ms |
6748 KB |
Output is correct |
23 |
Correct |
2 ms |
6492 KB |
Output is correct |
24 |
Correct |
2 ms |
6492 KB |
Output is correct |
25 |
Correct |
2 ms |
6748 KB |
Output is correct |
26 |
Correct |
2 ms |
6748 KB |
Output is correct |
27 |
Correct |
3 ms |
6748 KB |
Output is correct |
28 |
Correct |
2 ms |
6748 KB |
Output is correct |
29 |
Correct |
2 ms |
6748 KB |
Output is correct |
30 |
Correct |
3 ms |
6748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
59 ms |
17688 KB |
Output is correct |
3 |
Correct |
85 ms |
17476 KB |
Output is correct |
4 |
Correct |
60 ms |
17492 KB |
Output is correct |
5 |
Correct |
78 ms |
17492 KB |
Output is correct |
6 |
Correct |
61 ms |
17556 KB |
Output is correct |
7 |
Correct |
61 ms |
17632 KB |
Output is correct |
8 |
Correct |
1 ms |
6488 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6492 KB |
Output is correct |
11 |
Correct |
1 ms |
6488 KB |
Output is correct |
12 |
Correct |
1 ms |
6492 KB |
Output is correct |
13 |
Correct |
1 ms |
6488 KB |
Output is correct |
14 |
Correct |
2 ms |
6492 KB |
Output is correct |
15 |
Correct |
2 ms |
6492 KB |
Output is correct |
16 |
Correct |
3 ms |
6492 KB |
Output is correct |
17 |
Correct |
2 ms |
6492 KB |
Output is correct |
18 |
Correct |
2 ms |
6492 KB |
Output is correct |
19 |
Correct |
2 ms |
6504 KB |
Output is correct |
20 |
Correct |
2 ms |
6492 KB |
Output is correct |
21 |
Correct |
2 ms |
6492 KB |
Output is correct |
22 |
Correct |
2 ms |
6748 KB |
Output is correct |
23 |
Correct |
2 ms |
6748 KB |
Output is correct |
24 |
Correct |
2 ms |
6756 KB |
Output is correct |
25 |
Correct |
2 ms |
6748 KB |
Output is correct |
26 |
Correct |
2 ms |
6744 KB |
Output is correct |
27 |
Correct |
2 ms |
6492 KB |
Output is correct |
28 |
Correct |
2 ms |
6492 KB |
Output is correct |
29 |
Correct |
2 ms |
6588 KB |
Output is correct |
30 |
Correct |
2 ms |
6748 KB |
Output is correct |
31 |
Correct |
2 ms |
6492 KB |
Output is correct |
32 |
Correct |
2 ms |
6492 KB |
Output is correct |
33 |
Correct |
2 ms |
6748 KB |
Output is correct |
34 |
Correct |
2 ms |
6748 KB |
Output is correct |
35 |
Correct |
3 ms |
6748 KB |
Output is correct |
36 |
Correct |
2 ms |
6748 KB |
Output is correct |
37 |
Correct |
2 ms |
6748 KB |
Output is correct |
38 |
Correct |
3 ms |
6748 KB |
Output is correct |
39 |
Correct |
147 ms |
17748 KB |
Output is correct |
40 |
Correct |
103 ms |
17992 KB |
Output is correct |
41 |
Correct |
89 ms |
17744 KB |
Output is correct |
42 |
Correct |
82 ms |
17752 KB |
Output is correct |
43 |
Correct |
85 ms |
17488 KB |
Output is correct |
44 |
Correct |
107 ms |
17704 KB |
Output is correct |
45 |
Correct |
87 ms |
17724 KB |
Output is correct |
46 |
Correct |
91 ms |
17748 KB |
Output is correct |
47 |
Correct |
91 ms |
17700 KB |
Output is correct |
48 |
Correct |
94 ms |
17744 KB |
Output is correct |
49 |
Correct |
132 ms |
17744 KB |
Output is correct |
50 |
Correct |
108 ms |
17632 KB |
Output is correct |
51 |
Correct |
138 ms |
17748 KB |
Output is correct |
52 |
Correct |
199 ms |
20160 KB |
Output is correct |
53 |
Correct |
249 ms |
20240 KB |
Output is correct |
54 |
Correct |
153 ms |
20072 KB |
Output is correct |
55 |
Correct |
253 ms |
20064 KB |
Output is correct |
56 |
Correct |
214 ms |
22992 KB |
Output is correct |
57 |
Correct |
264 ms |
23308 KB |
Output is correct |
58 |
Correct |
286 ms |
22976 KB |
Output is correct |
59 |
Correct |
291 ms |
22952 KB |
Output is correct |
60 |
Correct |
213 ms |
23092 KB |
Output is correct |
61 |
Correct |
231 ms |
23132 KB |
Output is correct |
62 |
Correct |
196 ms |
22912 KB |
Output is correct |
63 |
Correct |
235 ms |
23128 KB |
Output is correct |
64 |
Correct |
202 ms |
23796 KB |
Output is correct |
65 |
Correct |
212 ms |
23004 KB |
Output is correct |
66 |
Correct |
250 ms |
23452 KB |
Output is correct |
67 |
Correct |
299 ms |
22976 KB |
Output is correct |