#include <bits/stdc++.h>
using namespace std;
#define pii pair<int, int>
const int maxN = 2e5 + 5, inf = 1e9;
int n, m, a[maxN]; pii dis[3][maxN];
vector<int> adj[maxN];
void dijkstra(int type, int st) {
priority_queue<pair<pii, int>, vector<pair<pii, int>>, greater<pair<pii, int>>> pq;
dis[type][st] = {0, 0};
pq.push({dis[type][st], st});
while (pq.size() > 0) {
int len = pq.top().first.first, height = pq.top().first.second, u = pq.top().second; pq.pop();
if (dis[type][u].first != len || dis[type][u].second != height) continue;
for (auto v: adj[u]) {
pair<int, int> nw = {len + max(1, a[v] - len), max(height, a[v])};
if (dis[type][v] > nw) {
dis[type][v] = nw;
pq.push({nw, v});
}
}
}
}
int main() {
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> a[i];
dis[1][i] = dis[2][i] = {inf, 0};
}
for (int i = 1; i <= m; i++) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
dijkstra(1, 1);
dijkstra(2, n);
int ans = inf;
for (int i = 1; i <= n; i++)
if (dis[1][i].second == a[i] && dis[2][i].second == a[i])
ans = min(ans, dis[1][i].first + dis[2][i].first);
cout << ans;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8784 KB |
Output is correct |
2 |
Correct |
49 ms |
16908 KB |
Output is correct |
3 |
Correct |
52 ms |
16712 KB |
Output is correct |
4 |
Correct |
52 ms |
16872 KB |
Output is correct |
5 |
Correct |
52 ms |
16708 KB |
Output is correct |
6 |
Correct |
52 ms |
16712 KB |
Output is correct |
7 |
Correct |
51 ms |
16712 KB |
Output is correct |
8 |
Correct |
2 ms |
8952 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8956 KB |
Output is correct |
2 |
Correct |
2 ms |
8952 KB |
Output is correct |
3 |
Correct |
2 ms |
8784 KB |
Output is correct |
4 |
Correct |
2 ms |
8784 KB |
Output is correct |
5 |
Correct |
2 ms |
8784 KB |
Output is correct |
6 |
Correct |
2 ms |
8784 KB |
Output is correct |
7 |
Correct |
3 ms |
8784 KB |
Output is correct |
8 |
Correct |
4 ms |
8784 KB |
Output is correct |
9 |
Correct |
2 ms |
8784 KB |
Output is correct |
10 |
Correct |
3 ms |
8784 KB |
Output is correct |
11 |
Correct |
3 ms |
8784 KB |
Output is correct |
12 |
Correct |
3 ms |
8784 KB |
Output is correct |
13 |
Correct |
3 ms |
8784 KB |
Output is correct |
14 |
Correct |
3 ms |
8956 KB |
Output is correct |
15 |
Correct |
4 ms |
8784 KB |
Output is correct |
16 |
Correct |
4 ms |
8952 KB |
Output is correct |
17 |
Correct |
3 ms |
8784 KB |
Output is correct |
18 |
Correct |
2 ms |
8784 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8956 KB |
Output is correct |
2 |
Correct |
2 ms |
8952 KB |
Output is correct |
3 |
Correct |
2 ms |
8784 KB |
Output is correct |
4 |
Correct |
2 ms |
8784 KB |
Output is correct |
5 |
Correct |
2 ms |
8784 KB |
Output is correct |
6 |
Correct |
2 ms |
8784 KB |
Output is correct |
7 |
Correct |
3 ms |
8784 KB |
Output is correct |
8 |
Correct |
4 ms |
8784 KB |
Output is correct |
9 |
Correct |
2 ms |
8784 KB |
Output is correct |
10 |
Correct |
3 ms |
8784 KB |
Output is correct |
11 |
Correct |
3 ms |
8784 KB |
Output is correct |
12 |
Correct |
3 ms |
8784 KB |
Output is correct |
13 |
Correct |
3 ms |
8784 KB |
Output is correct |
14 |
Correct |
3 ms |
8956 KB |
Output is correct |
15 |
Correct |
4 ms |
8784 KB |
Output is correct |
16 |
Correct |
4 ms |
8952 KB |
Output is correct |
17 |
Correct |
3 ms |
8784 KB |
Output is correct |
18 |
Correct |
2 ms |
8784 KB |
Output is correct |
19 |
Correct |
3 ms |
8784 KB |
Output is correct |
20 |
Correct |
3 ms |
8784 KB |
Output is correct |
21 |
Correct |
3 ms |
8784 KB |
Output is correct |
22 |
Correct |
4 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 |
3 ms |
8784 KB |
Output is correct |
27 |
Correct |
3 ms |
8784 KB |
Output is correct |
28 |
Correct |
3 ms |
8784 KB |
Output is correct |
29 |
Correct |
4 ms |
8784 KB |
Output is correct |
30 |
Correct |
3 ms |
8784 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8784 KB |
Output is correct |
2 |
Correct |
49 ms |
16908 KB |
Output is correct |
3 |
Correct |
52 ms |
16712 KB |
Output is correct |
4 |
Correct |
52 ms |
16872 KB |
Output is correct |
5 |
Correct |
52 ms |
16708 KB |
Output is correct |
6 |
Correct |
52 ms |
16712 KB |
Output is correct |
7 |
Correct |
51 ms |
16712 KB |
Output is correct |
8 |
Correct |
2 ms |
8952 KB |
Output is correct |
9 |
Correct |
2 ms |
8956 KB |
Output is correct |
10 |
Correct |
2 ms |
8952 KB |
Output is correct |
11 |
Correct |
2 ms |
8784 KB |
Output is correct |
12 |
Correct |
2 ms |
8784 KB |
Output is correct |
13 |
Correct |
2 ms |
8784 KB |
Output is correct |
14 |
Correct |
2 ms |
8784 KB |
Output is correct |
15 |
Correct |
3 ms |
8784 KB |
Output is correct |
16 |
Correct |
4 ms |
8784 KB |
Output is correct |
17 |
Correct |
2 ms |
8784 KB |
Output is correct |
18 |
Correct |
3 ms |
8784 KB |
Output is correct |
19 |
Correct |
3 ms |
8784 KB |
Output is correct |
20 |
Correct |
3 ms |
8784 KB |
Output is correct |
21 |
Correct |
3 ms |
8784 KB |
Output is correct |
22 |
Correct |
3 ms |
8956 KB |
Output is correct |
23 |
Correct |
4 ms |
8784 KB |
Output is correct |
24 |
Correct |
4 ms |
8952 KB |
Output is correct |
25 |
Correct |
3 ms |
8784 KB |
Output is correct |
26 |
Correct |
2 ms |
8784 KB |
Output is correct |
27 |
Correct |
3 ms |
8784 KB |
Output is correct |
28 |
Correct |
3 ms |
8784 KB |
Output is correct |
29 |
Correct |
3 ms |
8784 KB |
Output is correct |
30 |
Correct |
4 ms |
8784 KB |
Output is correct |
31 |
Correct |
3 ms |
8784 KB |
Output is correct |
32 |
Correct |
3 ms |
8784 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 |
8784 KB |
Output is correct |
37 |
Correct |
4 ms |
8784 KB |
Output is correct |
38 |
Correct |
3 ms |
8784 KB |
Output is correct |
39 |
Correct |
103 ms |
16788 KB |
Output is correct |
40 |
Correct |
92 ms |
16736 KB |
Output is correct |
41 |
Correct |
90 ms |
16712 KB |
Output is correct |
42 |
Correct |
97 ms |
16812 KB |
Output is correct |
43 |
Correct |
94 ms |
16712 KB |
Output is correct |
44 |
Correct |
81 ms |
16712 KB |
Output is correct |
45 |
Correct |
92 ms |
16712 KB |
Output is correct |
46 |
Correct |
101 ms |
16884 KB |
Output is correct |
47 |
Correct |
93 ms |
16712 KB |
Output is correct |
48 |
Correct |
86 ms |
16712 KB |
Output is correct |
49 |
Correct |
116 ms |
16968 KB |
Output is correct |
50 |
Correct |
108 ms |
16712 KB |
Output is correct |
51 |
Correct |
102 ms |
16712 KB |
Output is correct |
52 |
Correct |
179 ms |
18876 KB |
Output is correct |
53 |
Correct |
147 ms |
18808 KB |
Output is correct |
54 |
Correct |
151 ms |
18964 KB |
Output is correct |
55 |
Correct |
158 ms |
18960 KB |
Output is correct |
56 |
Correct |
194 ms |
21508 KB |
Output is correct |
57 |
Correct |
222 ms |
21244 KB |
Output is correct |
58 |
Correct |
210 ms |
21352 KB |
Output is correct |
59 |
Correct |
205 ms |
21496 KB |
Output is correct |
60 |
Correct |
198 ms |
21360 KB |
Output is correct |
61 |
Correct |
226 ms |
21496 KB |
Output is correct |
62 |
Correct |
201 ms |
21500 KB |
Output is correct |
63 |
Correct |
247 ms |
21248 KB |
Output is correct |
64 |
Correct |
184 ms |
21312 KB |
Output is correct |
65 |
Correct |
188 ms |
21508 KB |
Output is correct |
66 |
Correct |
230 ms |
21476 KB |
Output is correct |
67 |
Correct |
239 ms |
21292 KB |
Output is correct |