#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m, x;
cin >> n >> m >> x;
vector<int> h(n);
for (int i = 0; i < n; i++) {
cin >> h[i];
}
vector<vector<pair<int, int>>> g(n);
for (int i = 0; i < m; i++) {
int u, v, w;
cin >> u >> v >> w;
--u; --v;
if (w <= h[u]) {
g[u].emplace_back(v, w);
}
if (w <= h[v]) {
g[v].emplace_back(u, w);
}
}
const long long inf = (long long) 1e18;
vector<vector<long long>> dist(n, vector<long long>(2, inf));
dist[0][1] = 0;
dist[0][0] = x;
vector<int> cur(n);
cur[0] = x;
set<tuple<long long, int, int>> st;
st.emplace(dist[0][0], 0, 0);
st.emplace(dist[0][1], 0, 1);
vector<vector<bool>> was(n, vector<bool>(2));
while (!st.empty()) {
auto it = st.begin();
long long d = get<0>(*it);
int v = get<1>(*it);
int t = get<2>(*it);
st.erase(it);
if (was[v][t] || d != dist[v][t]) {
continue;
}
was[v][t] = true;
int c = (t == 0 ? 0 : cur[v]);
for (auto& e : g[v]) {
int u = e.first;
int w = e.second;
if (c <= w) {
if (dist[u][0] > dist[v][t] + 2 * w - c) {
dist[u][0] = dist[v][t] + 2 * w - c;
st.emplace(dist[u][0], u, 0);
}
} else {
int nc = c - w;
if (dist[u][1] > dist[v][t] + w + max(0, nc - h[u])) {
dist[u][1] = dist[v][t] + w + max(0, nc - h[u]);
cur[u] = min(h[u], nc);
st.emplace(dist[u][1], u, 1);
}
}
}
}
long long res = min(dist[n - 1][0] + h[n - 1], dist[n - 1][1] + h[n - 1] - cur[n - 1]);
if (res >= inf) {
cout << -1 << '\n';
} else {
cout << res << '\n';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
2 ms |
540 KB |
Output is correct |
4 |
Correct |
1 ms |
612 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
476 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
600 KB |
Output is correct |
13 |
Correct |
1 ms |
600 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
1 ms |
600 KB |
Output is correct |
17 |
Correct |
1 ms |
604 KB |
Output is correct |
18 |
Correct |
1 ms |
604 KB |
Output is correct |
19 |
Correct |
0 ms |
604 KB |
Output is correct |
20 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
101 ms |
25936 KB |
Output is correct |
2 |
Correct |
93 ms |
18256 KB |
Output is correct |
3 |
Correct |
83 ms |
23868 KB |
Output is correct |
4 |
Correct |
127 ms |
23644 KB |
Output is correct |
5 |
Correct |
84 ms |
16564 KB |
Output is correct |
6 |
Correct |
3 ms |
600 KB |
Output is correct |
7 |
Correct |
58 ms |
21364 KB |
Output is correct |
8 |
Correct |
147 ms |
28964 KB |
Output is correct |
9 |
Correct |
64 ms |
17172 KB |
Output is correct |
10 |
Correct |
51 ms |
19536 KB |
Output is correct |
11 |
Correct |
12 ms |
4696 KB |
Output is correct |
12 |
Correct |
62 ms |
12336 KB |
Output is correct |
13 |
Correct |
25 ms |
6252 KB |
Output is correct |
14 |
Correct |
77 ms |
21584 KB |
Output is correct |
15 |
Correct |
4 ms |
2392 KB |
Output is correct |
16 |
Correct |
78 ms |
16140 KB |
Output is correct |
17 |
Correct |
11 ms |
5212 KB |
Output is correct |
18 |
Correct |
14 ms |
7880 KB |
Output is correct |
19 |
Correct |
26 ms |
9548 KB |
Output is correct |
20 |
Correct |
15 ms |
7768 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
2 ms |
540 KB |
Output is correct |
4 |
Correct |
1 ms |
612 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
476 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
600 KB |
Output is correct |
13 |
Correct |
1 ms |
600 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
1 ms |
600 KB |
Output is correct |
17 |
Correct |
1 ms |
604 KB |
Output is correct |
18 |
Correct |
1 ms |
604 KB |
Output is correct |
19 |
Correct |
0 ms |
604 KB |
Output is correct |
20 |
Correct |
1 ms |
604 KB |
Output is correct |
21 |
Correct |
101 ms |
25936 KB |
Output is correct |
22 |
Correct |
93 ms |
18256 KB |
Output is correct |
23 |
Correct |
83 ms |
23868 KB |
Output is correct |
24 |
Correct |
127 ms |
23644 KB |
Output is correct |
25 |
Correct |
84 ms |
16564 KB |
Output is correct |
26 |
Correct |
3 ms |
600 KB |
Output is correct |
27 |
Correct |
58 ms |
21364 KB |
Output is correct |
28 |
Correct |
147 ms |
28964 KB |
Output is correct |
29 |
Correct |
64 ms |
17172 KB |
Output is correct |
30 |
Correct |
51 ms |
19536 KB |
Output is correct |
31 |
Correct |
12 ms |
4696 KB |
Output is correct |
32 |
Correct |
62 ms |
12336 KB |
Output is correct |
33 |
Correct |
25 ms |
6252 KB |
Output is correct |
34 |
Correct |
77 ms |
21584 KB |
Output is correct |
35 |
Correct |
4 ms |
2392 KB |
Output is correct |
36 |
Correct |
78 ms |
16140 KB |
Output is correct |
37 |
Correct |
11 ms |
5212 KB |
Output is correct |
38 |
Correct |
14 ms |
7880 KB |
Output is correct |
39 |
Correct |
26 ms |
9548 KB |
Output is correct |
40 |
Correct |
15 ms |
7768 KB |
Output is correct |
41 |
Correct |
58 ms |
10832 KB |
Output is correct |
42 |
Correct |
23 ms |
15444 KB |
Output is correct |
43 |
Correct |
45 ms |
12512 KB |
Output is correct |
44 |
Correct |
81 ms |
15224 KB |
Output is correct |
45 |
Correct |
113 ms |
19284 KB |
Output is correct |
46 |
Correct |
16 ms |
16596 KB |
Output is correct |
47 |
Correct |
194 ms |
30036 KB |
Output is correct |
48 |
Correct |
135 ms |
30436 KB |
Output is correct |
49 |
Correct |
109 ms |
27516 KB |
Output is correct |
50 |
Correct |
123 ms |
21588 KB |
Output is correct |
51 |
Correct |
21 ms |
8024 KB |
Output is correct |
52 |
Correct |
109 ms |
19284 KB |
Output is correct |
53 |
Correct |
57 ms |
21840 KB |
Output is correct |
54 |
Correct |
72 ms |
19176 KB |
Output is correct |
55 |
Correct |
66 ms |
21588 KB |
Output is correct |
56 |
Correct |
159 ms |
30900 KB |
Output is correct |
57 |
Correct |
38 ms |
19024 KB |
Output is correct |
58 |
Correct |
3 ms |
1628 KB |
Output is correct |
59 |
Correct |
75 ms |
18972 KB |
Output is correct |
60 |
Correct |
37 ms |
18780 KB |
Output is correct |