#include<bits/stdc++.h>
using namespace std;
#define int long long
#define F first
#define S second
#define all(x) x.begin(),x.end()
#define pii pair<int,int>
#define pb push_back
#define sz(x) (int)(x.size())
#define chmin(x,y) x=min(x,y)
#define chmax(x,y) x=max(x,y)
#define vi vector<int>
#define vp vector<pii>
#define vvi vector<vi>
#define ykh mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count())
#define __lg(x) 63-__builtin_clzll(x)
#define pow2(x) (1LL<<x)
void __print(int x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifdef local
void setio(){freopen("/Users/iantsai/Library/Mobile Documents/com~apple~CloudDocs/cpp/Empty.md","r",stdin);}
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
void setio(){}
#define debug(x...)
#endif
void setIO(string s) {
freopen((s + ".in").c_str(), "r", stdin);
freopen((s + ".out").c_str(), "w", stdout);
}
struct line{
int a,b;
int operator()(const int x)const{
return a*x+b;
}
};
bool check(line l1,line l2,line l3){
return (l3.b-l2.b)*(l1.a-l2.a)<=(l2.b-l1.b)*(l2.a-l3.a);
}
const int mxn=2e5+5;
const int inf=4e18;
int h[mxn];
vector<int>adj[mxn];
int n;
int dis[mxn][2];
int ans;
void dij(int s,int f){
for(int i=1;i<=n;i++){
dis[i][f]=inf;
}
dis[s][f]=0;
priority_queue<pii,vector<pii>,greater<pii>>pq;
pq.push({0,s});
while(sz(pq)){
auto [d,v]=pq.top();
pq.pop();
if(d>dis[v][f])continue;
for(auto u:adj[v]){
if(max(h[u],dis[v][f]+1)<dis[u][f]){
dis[u][f]=max(h[u],dis[v][f]+1);
pq.push({dis[u][f],u});
}
}
}
if(f){
for(int i=1;i<=n;i++){
ans=min(ans,dis[i][0]+dis[i][1]+abs(dis[i][0]-dis[i][1]));
for(auto u:adj[i]){
ans=min(ans,1+dis[i][0]+dis[u][1]+abs(dis[u][1]-dis[i][0]));
}
}
}
}
signed main(){
setio();
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
int m;
ans=inf;
cin>>n>>m;
for(int i=1;i<=n;i++){
cin>>h[i];
}
for(int i=0;i<m;i++){
int a,b;
cin>>a>>b;
adj[a].pb(b);
adj[b].pb(a);
}
dij(1,0);
dij(n,1);
for(int i=1;i<=n;i++){
debug(i,dis[i][0],dis[i][1]);
}
cout<<ans<<'\n';
}
/*
input:
*/
Compilation message
Main.cpp: In function 'void dij(long long int, long long int)':
Main.cpp:69:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
69 | auto [d,v]=pq.top();
| ^
Main.cpp: In function 'void setIO(std::string)':
Main.cpp:42:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
42 | freopen((s + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:43:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
43 | freopen((s + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
60 ms |
19232 KB |
Output is correct |
3 |
Correct |
60 ms |
19476 KB |
Output is correct |
4 |
Correct |
66 ms |
20104 KB |
Output is correct |
5 |
Correct |
66 ms |
20304 KB |
Output is correct |
6 |
Correct |
63 ms |
20324 KB |
Output is correct |
7 |
Correct |
63 ms |
20308 KB |
Output is correct |
8 |
Correct |
1 ms |
6488 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6492 KB |
Output is correct |
2 |
Correct |
2 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
2 ms |
6748 KB |
Output is correct |
7 |
Correct |
2 ms |
6748 KB |
Output is correct |
8 |
Correct |
2 ms |
6748 KB |
Output is correct |
9 |
Correct |
2 ms |
6492 KB |
Output is correct |
10 |
Correct |
2 ms |
6748 KB |
Output is correct |
11 |
Correct |
2 ms |
6748 KB |
Output is correct |
12 |
Correct |
2 ms |
6748 KB |
Output is correct |
13 |
Correct |
2 ms |
6748 KB |
Output is correct |
14 |
Correct |
3 ms |
6748 KB |
Output is correct |
15 |
Correct |
3 ms |
6748 KB |
Output is correct |
16 |
Correct |
3 ms |
6748 KB |
Output is correct |
17 |
Correct |
3 ms |
6748 KB |
Output is correct |
18 |
Correct |
2 ms |
6492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6492 KB |
Output is correct |
2 |
Correct |
2 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
2 ms |
6748 KB |
Output is correct |
7 |
Correct |
2 ms |
6748 KB |
Output is correct |
8 |
Correct |
2 ms |
6748 KB |
Output is correct |
9 |
Correct |
2 ms |
6492 KB |
Output is correct |
10 |
Correct |
2 ms |
6748 KB |
Output is correct |
11 |
Correct |
2 ms |
6748 KB |
Output is correct |
12 |
Correct |
2 ms |
6748 KB |
Output is correct |
13 |
Correct |
2 ms |
6748 KB |
Output is correct |
14 |
Correct |
3 ms |
6748 KB |
Output is correct |
15 |
Correct |
3 ms |
6748 KB |
Output is correct |
16 |
Correct |
3 ms |
6748 KB |
Output is correct |
17 |
Correct |
3 ms |
6748 KB |
Output is correct |
18 |
Correct |
2 ms |
6492 KB |
Output is correct |
19 |
Correct |
3 ms |
6748 KB |
Output is correct |
20 |
Correct |
3 ms |
6748 KB |
Output is correct |
21 |
Correct |
3 ms |
6744 KB |
Output is correct |
22 |
Correct |
3 ms |
6656 KB |
Output is correct |
23 |
Correct |
2 ms |
6636 KB |
Output is correct |
24 |
Correct |
2 ms |
6748 KB |
Output is correct |
25 |
Correct |
3 ms |
6752 KB |
Output is correct |
26 |
Correct |
3 ms |
6744 KB |
Output is correct |
27 |
Correct |
3 ms |
6748 KB |
Output is correct |
28 |
Correct |
3 ms |
6876 KB |
Output is correct |
29 |
Correct |
3 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 |
60 ms |
19232 KB |
Output is correct |
3 |
Correct |
60 ms |
19476 KB |
Output is correct |
4 |
Correct |
66 ms |
20104 KB |
Output is correct |
5 |
Correct |
66 ms |
20304 KB |
Output is correct |
6 |
Correct |
63 ms |
20324 KB |
Output is correct |
7 |
Correct |
63 ms |
20308 KB |
Output is correct |
8 |
Correct |
1 ms |
6488 KB |
Output is correct |
9 |
Correct |
2 ms |
6492 KB |
Output is correct |
10 |
Correct |
2 ms |
6492 KB |
Output is correct |
11 |
Correct |
1 ms |
6492 KB |
Output is correct |
12 |
Correct |
1 ms |
6492 KB |
Output is correct |
13 |
Correct |
1 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 |
6748 KB |
Output is correct |
17 |
Correct |
2 ms |
6492 KB |
Output is correct |
18 |
Correct |
2 ms |
6748 KB |
Output is correct |
19 |
Correct |
2 ms |
6748 KB |
Output is correct |
20 |
Correct |
2 ms |
6748 KB |
Output is correct |
21 |
Correct |
2 ms |
6748 KB |
Output is correct |
22 |
Correct |
3 ms |
6748 KB |
Output is correct |
23 |
Correct |
3 ms |
6748 KB |
Output is correct |
24 |
Correct |
3 ms |
6748 KB |
Output is correct |
25 |
Correct |
3 ms |
6748 KB |
Output is correct |
26 |
Correct |
2 ms |
6492 KB |
Output is correct |
27 |
Correct |
3 ms |
6748 KB |
Output is correct |
28 |
Correct |
3 ms |
6748 KB |
Output is correct |
29 |
Correct |
3 ms |
6744 KB |
Output is correct |
30 |
Correct |
3 ms |
6656 KB |
Output is correct |
31 |
Correct |
2 ms |
6636 KB |
Output is correct |
32 |
Correct |
2 ms |
6748 KB |
Output is correct |
33 |
Correct |
3 ms |
6752 KB |
Output is correct |
34 |
Correct |
3 ms |
6744 KB |
Output is correct |
35 |
Correct |
3 ms |
6748 KB |
Output is correct |
36 |
Correct |
3 ms |
6876 KB |
Output is correct |
37 |
Correct |
3 ms |
6748 KB |
Output is correct |
38 |
Correct |
3 ms |
6748 KB |
Output is correct |
39 |
Correct |
150 ms |
21264 KB |
Output is correct |
40 |
Correct |
127 ms |
20724 KB |
Output is correct |
41 |
Correct |
118 ms |
20584 KB |
Output is correct |
42 |
Correct |
117 ms |
20588 KB |
Output is correct |
43 |
Correct |
113 ms |
20612 KB |
Output is correct |
44 |
Correct |
128 ms |
20720 KB |
Output is correct |
45 |
Correct |
99 ms |
20308 KB |
Output is correct |
46 |
Correct |
98 ms |
20588 KB |
Output is correct |
47 |
Correct |
96 ms |
20560 KB |
Output is correct |
48 |
Correct |
120 ms |
20416 KB |
Output is correct |
49 |
Correct |
185 ms |
21452 KB |
Output is correct |
50 |
Correct |
132 ms |
21208 KB |
Output is correct |
51 |
Correct |
138 ms |
20960 KB |
Output is correct |
52 |
Correct |
240 ms |
27144 KB |
Output is correct |
53 |
Correct |
217 ms |
25912 KB |
Output is correct |
54 |
Correct |
217 ms |
25920 KB |
Output is correct |
55 |
Correct |
268 ms |
26244 KB |
Output is correct |
56 |
Correct |
332 ms |
31060 KB |
Output is correct |
57 |
Correct |
283 ms |
32488 KB |
Output is correct |
58 |
Correct |
338 ms |
32432 KB |
Output is correct |
59 |
Correct |
286 ms |
32680 KB |
Output is correct |
60 |
Correct |
293 ms |
32204 KB |
Output is correct |
61 |
Correct |
287 ms |
31932 KB |
Output is correct |
62 |
Correct |
295 ms |
30972 KB |
Output is correct |
63 |
Correct |
240 ms |
31088 KB |
Output is correct |
64 |
Correct |
271 ms |
31420 KB |
Output is correct |
65 |
Correct |
258 ms |
31200 KB |
Output is correct |
66 |
Correct |
284 ms |
31336 KB |
Output is correct |
67 |
Correct |
281 ms |
31404 KB |
Output is correct |