#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 = min(abs(tstart[u] - ealt[u]), abs(tend[u] - salt[u]));
if(salt[u] <= ealt[u] && ealt[u] <= tstart[u]) ret = 0;
if(salt[u] <= tend[u] && tend[u] <= tstart[u]) ret = 0;
if(ealt[u] <= salt[u] && salt[u] <= tend[u]) ret = 0;
if(ealt[u] <= tstart[u] && tstart[u] <= tend[u]) ret = 0;
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:64:8: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
64 | auto [time, minalt, u] = q.top(); q.pop();
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
133 ms |
15284 KB |
Output is correct |
3 |
Correct |
142 ms |
15248 KB |
Output is correct |
4 |
Correct |
147 ms |
15292 KB |
Output is correct |
5 |
Correct |
156 ms |
15296 KB |
Output is correct |
6 |
Correct |
161 ms |
15284 KB |
Output is correct |
7 |
Correct |
175 ms |
15148 KB |
Output is correct |
8 |
Correct |
1 ms |
600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
2 ms |
604 KB |
Output is correct |
7 |
Correct |
2 ms |
604 KB |
Output is correct |
8 |
Correct |
2 ms |
604 KB |
Output is correct |
9 |
Correct |
2 ms |
856 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 |
600 KB |
Output is correct |
13 |
Correct |
2 ms |
600 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 |
# |
결과 |
실행 시간 |
메모리 |
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 |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
2 ms |
604 KB |
Output is correct |
7 |
Correct |
2 ms |
604 KB |
Output is correct |
8 |
Correct |
2 ms |
604 KB |
Output is correct |
9 |
Correct |
2 ms |
856 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 |
600 KB |
Output is correct |
13 |
Correct |
2 ms |
600 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 |
600 KB |
Output is correct |
20 |
Correct |
2 ms |
604 KB |
Output is correct |
21 |
Correct |
2 ms |
604 KB |
Output is correct |
22 |
Correct |
2 ms |
476 KB |
Output is correct |
23 |
Correct |
2 ms |
604 KB |
Output is correct |
24 |
Correct |
2 ms |
604 KB |
Output is correct |
25 |
Correct |
2 ms |
604 KB |
Output is correct |
26 |
Correct |
3 ms |
604 KB |
Output is correct |
27 |
Correct |
3 ms |
604 KB |
Output is correct |
28 |
Correct |
3 ms |
612 KB |
Output is correct |
29 |
Correct |
3 ms |
604 KB |
Output is correct |
30 |
Correct |
3 ms |
600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
133 ms |
15284 KB |
Output is correct |
3 |
Correct |
142 ms |
15248 KB |
Output is correct |
4 |
Correct |
147 ms |
15292 KB |
Output is correct |
5 |
Correct |
156 ms |
15296 KB |
Output is correct |
6 |
Correct |
161 ms |
15284 KB |
Output is correct |
7 |
Correct |
175 ms |
15148 KB |
Output is correct |
8 |
Correct |
1 ms |
600 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 |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
2 ms |
604 KB |
Output is correct |
15 |
Correct |
2 ms |
604 KB |
Output is correct |
16 |
Correct |
2 ms |
604 KB |
Output is correct |
17 |
Correct |
2 ms |
856 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 |
600 KB |
Output is correct |
21 |
Correct |
2 ms |
600 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 |
600 KB |
Output is correct |
28 |
Correct |
2 ms |
604 KB |
Output is correct |
29 |
Correct |
2 ms |
604 KB |
Output is correct |
30 |
Correct |
2 ms |
476 KB |
Output is correct |
31 |
Correct |
2 ms |
604 KB |
Output is correct |
32 |
Correct |
2 ms |
604 KB |
Output is correct |
33 |
Correct |
2 ms |
604 KB |
Output is correct |
34 |
Correct |
3 ms |
604 KB |
Output is correct |
35 |
Correct |
3 ms |
604 KB |
Output is correct |
36 |
Correct |
3 ms |
612 KB |
Output is correct |
37 |
Correct |
3 ms |
604 KB |
Output is correct |
38 |
Correct |
3 ms |
600 KB |
Output is correct |
39 |
Correct |
275 ms |
17748 KB |
Output is correct |
40 |
Correct |
198 ms |
17656 KB |
Output is correct |
41 |
Correct |
191 ms |
17492 KB |
Output is correct |
42 |
Correct |
214 ms |
17536 KB |
Output is correct |
43 |
Correct |
188 ms |
17492 KB |
Output is correct |
44 |
Correct |
200 ms |
17604 KB |
Output is correct |
45 |
Correct |
216 ms |
17700 KB |
Output is correct |
46 |
Correct |
234 ms |
17708 KB |
Output is correct |
47 |
Correct |
211 ms |
17504 KB |
Output is correct |
48 |
Correct |
188 ms |
17488 KB |
Output is correct |
49 |
Correct |
292 ms |
17844 KB |
Output is correct |
50 |
Correct |
296 ms |
17488 KB |
Output is correct |
51 |
Correct |
316 ms |
17808 KB |
Output is correct |
52 |
Correct |
406 ms |
20052 KB |
Output is correct |
53 |
Correct |
346 ms |
19356 KB |
Output is correct |
54 |
Correct |
370 ms |
19480 KB |
Output is correct |
55 |
Correct |
367 ms |
19828 KB |
Output is correct |
56 |
Correct |
471 ms |
21704 KB |
Output is correct |
57 |
Correct |
472 ms |
21724 KB |
Output is correct |
58 |
Correct |
477 ms |
22388 KB |
Output is correct |
59 |
Correct |
455 ms |
22292 KB |
Output is correct |
60 |
Correct |
442 ms |
22592 KB |
Output is correct |
61 |
Correct |
398 ms |
21964 KB |
Output is correct |
62 |
Correct |
444 ms |
21636 KB |
Output is correct |
63 |
Correct |
399 ms |
21636 KB |
Output is correct |
64 |
Correct |
374 ms |
21704 KB |
Output is correct |
65 |
Correct |
372 ms |
21840 KB |
Output is correct |
66 |
Correct |
414 ms |
21700 KB |
Output is correct |
67 |
Correct |
430 ms |
21960 KB |
Output is correct |