#include <bits/stdc++.h>
using namespace std;
const int N = 1e5+5;
using ll = long long;
int n, m, x, h[N]; ll mxh[N];
vector <pair <int, int>> adj[N];
int main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n >> m >> x;
for(int i = 1; i <= n; ++i) cin >> h[i];
for(int i = 1; i <= m; ++i) {
int u, v, w; cin >> u >> v >> w;
if(h[u] >= w) adj[u].emplace_back(v, w);
if(h[v] >= w) adj[v].emplace_back(u, w);
}
fill(mxh+1, mxh+1+n, -1e18);
mxh[1] = x;
priority_queue <pair <ll, int>> pq;
pq.push({x, 1});
while(pq.size()) {
pair <ll, int> tp = pq.top(); pq.pop();
ll hu = tp.first; int u = tp.second;
if(hu < mxh[u]) continue;
for(pair <int, int> i : adj[u]) {
int v = i.first, w = i.second;
ll hv = min(hu-w, (ll)h[v]);
if(hv > mxh[v]) {
mxh[v] = hv;
pq.push({hv, v});
}
}
}
if(mxh[n] == (ll)-1e18) cout << "-1\n";
else cout << x + h[n] - 2*mxh[n] << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2908 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
2652 KB |
Output is correct |
8 |
Correct |
1 ms |
2652 KB |
Output is correct |
9 |
Correct |
1 ms |
2652 KB |
Output is correct |
10 |
Correct |
1 ms |
2652 KB |
Output is correct |
11 |
Correct |
1 ms |
2652 KB |
Output is correct |
12 |
Correct |
1 ms |
2652 KB |
Output is correct |
13 |
Correct |
1 ms |
2908 KB |
Output is correct |
14 |
Correct |
1 ms |
2652 KB |
Output is correct |
15 |
Correct |
1 ms |
2648 KB |
Output is correct |
16 |
Correct |
1 ms |
2648 KB |
Output is correct |
17 |
Correct |
1 ms |
2652 KB |
Output is correct |
18 |
Correct |
1 ms |
2652 KB |
Output is correct |
19 |
Correct |
1 ms |
2652 KB |
Output is correct |
20 |
Correct |
1 ms |
2652 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
81 ms |
6936 KB |
Output is correct |
2 |
Correct |
75 ms |
12496 KB |
Output is correct |
3 |
Correct |
65 ms |
8400 KB |
Output is correct |
4 |
Correct |
94 ms |
12744 KB |
Output is correct |
5 |
Correct |
70 ms |
11900 KB |
Output is correct |
6 |
Correct |
4 ms |
2648 KB |
Output is correct |
7 |
Correct |
56 ms |
3964 KB |
Output is correct |
8 |
Correct |
89 ms |
11240 KB |
Output is correct |
9 |
Correct |
59 ms |
4692 KB |
Output is correct |
10 |
Correct |
38 ms |
3920 KB |
Output is correct |
11 |
Correct |
11 ms |
3928 KB |
Output is correct |
12 |
Correct |
54 ms |
7376 KB |
Output is correct |
13 |
Correct |
21 ms |
7004 KB |
Output is correct |
14 |
Correct |
56 ms |
4420 KB |
Output is correct |
15 |
Correct |
3 ms |
2908 KB |
Output is correct |
16 |
Correct |
72 ms |
15804 KB |
Output is correct |
17 |
Correct |
8 ms |
4188 KB |
Output is correct |
18 |
Correct |
10 ms |
4444 KB |
Output is correct |
19 |
Correct |
22 ms |
5976 KB |
Output is correct |
20 |
Correct |
11 ms |
4700 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2908 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
2652 KB |
Output is correct |
8 |
Correct |
1 ms |
2652 KB |
Output is correct |
9 |
Correct |
1 ms |
2652 KB |
Output is correct |
10 |
Correct |
1 ms |
2652 KB |
Output is correct |
11 |
Correct |
1 ms |
2652 KB |
Output is correct |
12 |
Correct |
1 ms |
2652 KB |
Output is correct |
13 |
Correct |
1 ms |
2908 KB |
Output is correct |
14 |
Correct |
1 ms |
2652 KB |
Output is correct |
15 |
Correct |
1 ms |
2648 KB |
Output is correct |
16 |
Correct |
1 ms |
2648 KB |
Output is correct |
17 |
Correct |
1 ms |
2652 KB |
Output is correct |
18 |
Correct |
1 ms |
2652 KB |
Output is correct |
19 |
Correct |
1 ms |
2652 KB |
Output is correct |
20 |
Correct |
1 ms |
2652 KB |
Output is correct |
21 |
Correct |
81 ms |
6936 KB |
Output is correct |
22 |
Correct |
75 ms |
12496 KB |
Output is correct |
23 |
Correct |
65 ms |
8400 KB |
Output is correct |
24 |
Correct |
94 ms |
12744 KB |
Output is correct |
25 |
Correct |
70 ms |
11900 KB |
Output is correct |
26 |
Correct |
4 ms |
2648 KB |
Output is correct |
27 |
Correct |
56 ms |
3964 KB |
Output is correct |
28 |
Correct |
89 ms |
11240 KB |
Output is correct |
29 |
Correct |
59 ms |
4692 KB |
Output is correct |
30 |
Correct |
38 ms |
3920 KB |
Output is correct |
31 |
Correct |
11 ms |
3928 KB |
Output is correct |
32 |
Correct |
54 ms |
7376 KB |
Output is correct |
33 |
Correct |
21 ms |
7004 KB |
Output is correct |
34 |
Correct |
56 ms |
4420 KB |
Output is correct |
35 |
Correct |
3 ms |
2908 KB |
Output is correct |
36 |
Correct |
72 ms |
15804 KB |
Output is correct |
37 |
Correct |
8 ms |
4188 KB |
Output is correct |
38 |
Correct |
10 ms |
4444 KB |
Output is correct |
39 |
Correct |
22 ms |
5976 KB |
Output is correct |
40 |
Correct |
11 ms |
4700 KB |
Output is correct |
41 |
Correct |
52 ms |
9048 KB |
Output is correct |
42 |
Correct |
13 ms |
5468 KB |
Output is correct |
43 |
Correct |
52 ms |
7764 KB |
Output is correct |
44 |
Correct |
37 ms |
9176 KB |
Output is correct |
45 |
Correct |
68 ms |
15820 KB |
Output is correct |
46 |
Correct |
6 ms |
4440 KB |
Output is correct |
47 |
Correct |
76 ms |
16076 KB |
Output is correct |
48 |
Correct |
97 ms |
16588 KB |
Output is correct |
49 |
Correct |
75 ms |
14028 KB |
Output is correct |
50 |
Correct |
73 ms |
17032 KB |
Output is correct |
51 |
Correct |
20 ms |
5208 KB |
Output is correct |
52 |
Correct |
76 ms |
13796 KB |
Output is correct |
53 |
Correct |
57 ms |
9756 KB |
Output is correct |
54 |
Correct |
74 ms |
10648 KB |
Output is correct |
55 |
Correct |
53 ms |
9552 KB |
Output is correct |
56 |
Correct |
94 ms |
16840 KB |
Output is correct |
57 |
Correct |
24 ms |
6800 KB |
Output is correct |
58 |
Correct |
3 ms |
3160 KB |
Output is correct |
59 |
Correct |
52 ms |
9252 KB |
Output is correct |
60 |
Correct |
22 ms |
6744 KB |
Output is correct |