#include <bits/stdc++.h>
#define pb push_back
using namespace std;
int main(){
int n, m;
cin >> n >> m;
vector<int> a(n);
for(int i = 0; i < n; i++){
cin >> a[i];
}
vector<vector<int>> G(n);
for(int i = 0; i < m; i++){
int a, b;
cin >> a >> b;
--a, --b;
G[a].pb(b);
G[b].pb(a);
}
// time, minalt, v
priority_queue<array<int, 3>, vector<array<int, 3>>, greater<array<int, 3>>> q;
vector<int> tstart(n), salt(n);
q.push({0, 0, 0});
vector<bool> vis(n);
vis[0] = 1;
int ans = INT_MAX;
while(!q.empty()){
auto [time, minalt, u] = q.top(); q.pop();
tstart[u] = time;
salt[u] = minalt;
for(int v : G[u]){
if(vis[v]) continue;
vis[v] = 1;
if(time < a[v] - 1){
q.push({a[v], a[v], v}); // kesinlikle v icin minimum sure
} else{
q.push({time+1, max(max(minalt-1, a[v]), 0), v});
}
}
}
q.push({0, 0, n-1});
vis.assign(n, 0);
vector<int> tend(n), ealt(n);
function<int(int)> altdif = [&](int u){
int ret = abs(salt[u] - ealt[u]);
return ret;
};
while(!q.empty()){
auto [time, minalt, u] = q.top(); q.pop();
tend[u] = time;
ealt[u] = minalt;
ans = min(ans, tend[u] + tstart[u] + altdif(u));
for(int v : G[u]){
if(vis[v]) continue;
vis[v] = 1;
if(time < a[v] - 1){
q.push({a[v], a[v], v}); // kesinlikle v icin minimum sure
} else{
q.push({time+1, max(max(minalt-1, a[v]), 0), v});
}
}
}
cout << ans << endl;
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:34:8: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
34 | auto [time, minalt, u] = q.top(); q.pop();
| ^
Main.cpp:58:8: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
58 | auto [time, minalt, u] = q.top(); q.pop();
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
127 ms |
15188 KB |
Output is correct |
3 |
Correct |
136 ms |
15160 KB |
Output is correct |
4 |
Correct |
139 ms |
15184 KB |
Output is correct |
5 |
Correct |
149 ms |
15284 KB |
Output is correct |
6 |
Correct |
182 ms |
15184 KB |
Output is correct |
7 |
Correct |
155 ms |
15188 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
2 ms |
604 KB |
Output is correct |
7 |
Correct |
2 ms |
600 KB |
Output is correct |
8 |
Correct |
2 ms |
600 KB |
Output is correct |
9 |
Correct |
2 ms |
604 KB |
Output is correct |
10 |
Correct |
2 ms |
604 KB |
Output is correct |
11 |
Correct |
2 ms |
604 KB |
Output is correct |
12 |
Correct |
3 ms |
604 KB |
Output is correct |
13 |
Correct |
2 ms |
604 KB |
Output is correct |
14 |
Correct |
2 ms |
604 KB |
Output is correct |
15 |
Correct |
3 ms |
604 KB |
Output is correct |
16 |
Correct |
3 ms |
604 KB |
Output is correct |
17 |
Correct |
3 ms |
604 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
2 ms |
604 KB |
Output is correct |
7 |
Correct |
2 ms |
600 KB |
Output is correct |
8 |
Correct |
2 ms |
600 KB |
Output is correct |
9 |
Correct |
2 ms |
604 KB |
Output is correct |
10 |
Correct |
2 ms |
604 KB |
Output is correct |
11 |
Correct |
2 ms |
604 KB |
Output is correct |
12 |
Correct |
3 ms |
604 KB |
Output is correct |
13 |
Correct |
2 ms |
604 KB |
Output is correct |
14 |
Correct |
2 ms |
604 KB |
Output is correct |
15 |
Correct |
3 ms |
604 KB |
Output is correct |
16 |
Correct |
3 ms |
604 KB |
Output is correct |
17 |
Correct |
3 ms |
604 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
2 ms |
604 KB |
Output is correct |
20 |
Correct |
2 ms |
448 KB |
Output is correct |
21 |
Correct |
2 ms |
604 KB |
Output is correct |
22 |
Correct |
2 ms |
604 KB |
Output is correct |
23 |
Correct |
2 ms |
604 KB |
Output is correct |
24 |
Correct |
2 ms |
584 KB |
Output is correct |
25 |
Correct |
2 ms |
604 KB |
Output is correct |
26 |
Correct |
2 ms |
480 KB |
Output is correct |
27 |
Correct |
3 ms |
604 KB |
Output is correct |
28 |
Correct |
3 ms |
604 KB |
Output is correct |
29 |
Correct |
3 ms |
444 KB |
Output is correct |
30 |
Correct |
3 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
127 ms |
15188 KB |
Output is correct |
3 |
Correct |
136 ms |
15160 KB |
Output is correct |
4 |
Correct |
139 ms |
15184 KB |
Output is correct |
5 |
Correct |
149 ms |
15284 KB |
Output is correct |
6 |
Correct |
182 ms |
15184 KB |
Output is correct |
7 |
Correct |
155 ms |
15188 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
2 ms |
604 KB |
Output is correct |
15 |
Correct |
2 ms |
600 KB |
Output is correct |
16 |
Correct |
2 ms |
600 KB |
Output is correct |
17 |
Correct |
2 ms |
604 KB |
Output is correct |
18 |
Correct |
2 ms |
604 KB |
Output is correct |
19 |
Correct |
2 ms |
604 KB |
Output is correct |
20 |
Correct |
3 ms |
604 KB |
Output is correct |
21 |
Correct |
2 ms |
604 KB |
Output is correct |
22 |
Correct |
2 ms |
604 KB |
Output is correct |
23 |
Correct |
3 ms |
604 KB |
Output is correct |
24 |
Correct |
3 ms |
604 KB |
Output is correct |
25 |
Correct |
3 ms |
604 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
2 ms |
604 KB |
Output is correct |
28 |
Correct |
2 ms |
448 KB |
Output is correct |
29 |
Correct |
2 ms |
604 KB |
Output is correct |
30 |
Correct |
2 ms |
604 KB |
Output is correct |
31 |
Correct |
2 ms |
604 KB |
Output is correct |
32 |
Correct |
2 ms |
584 KB |
Output is correct |
33 |
Correct |
2 ms |
604 KB |
Output is correct |
34 |
Correct |
2 ms |
480 KB |
Output is correct |
35 |
Correct |
3 ms |
604 KB |
Output is correct |
36 |
Correct |
3 ms |
604 KB |
Output is correct |
37 |
Correct |
3 ms |
444 KB |
Output is correct |
38 |
Correct |
3 ms |
604 KB |
Output is correct |
39 |
Correct |
280 ms |
18088 KB |
Output is correct |
40 |
Correct |
173 ms |
17488 KB |
Output is correct |
41 |
Correct |
182 ms |
17348 KB |
Output is correct |
42 |
Correct |
175 ms |
17492 KB |
Output is correct |
43 |
Correct |
174 ms |
17488 KB |
Output is correct |
44 |
Correct |
174 ms |
17492 KB |
Output is correct |
45 |
Correct |
188 ms |
17452 KB |
Output is correct |
46 |
Correct |
182 ms |
17492 KB |
Output is correct |
47 |
Correct |
191 ms |
17492 KB |
Output is correct |
48 |
Correct |
193 ms |
17748 KB |
Output is correct |
49 |
Correct |
229 ms |
18244 KB |
Output is correct |
50 |
Correct |
214 ms |
17956 KB |
Output is correct |
51 |
Correct |
215 ms |
17836 KB |
Output is correct |
52 |
Correct |
297 ms |
20148 KB |
Output is correct |
53 |
Correct |
290 ms |
19660 KB |
Output is correct |
54 |
Correct |
315 ms |
19400 KB |
Output is correct |
55 |
Correct |
294 ms |
19408 KB |
Output is correct |
56 |
Correct |
372 ms |
21704 KB |
Output is correct |
57 |
Correct |
372 ms |
21708 KB |
Output is correct |
58 |
Correct |
366 ms |
22616 KB |
Output is correct |
59 |
Correct |
364 ms |
22468 KB |
Output is correct |
60 |
Correct |
361 ms |
22316 KB |
Output is correct |
61 |
Correct |
361 ms |
22632 KB |
Output is correct |
62 |
Correct |
337 ms |
21800 KB |
Output is correct |
63 |
Correct |
338 ms |
21704 KB |
Output is correct |
64 |
Correct |
343 ms |
21700 KB |
Output is correct |
65 |
Correct |
341 ms |
21700 KB |
Output is correct |
66 |
Correct |
349 ms |
21708 KB |
Output is correct |
67 |
Correct |
367 ms |
21704 KB |
Output is correct |