#include <bits/stdc++.h>
#define rep(i,a,b) for (auto i = (a); i <= (b); ++i)
#define Mup(x,y) x = max(x,y)
#define mup(x,y) x = min(x,y)
#define all(x) begin(x),end(x)
#define per(i,a,b) for (auto i = (b); i >= (a); --i)
using namespace std;
using ll = long long;
using ii = pair<int,int>;
#define dbg(...) fprintf(stderr,__VA_ARGS__)
const ll INF = 1e18;
const int N = 1e5+3;
int n, m, x, h[N];
vector<ii> adj[N];
ll dist[N];
int main() {
scanf("%d%d%d", &n, &m, &x);
rep(i,1,n) scanf("%d", &h[i]);
rep(i,1,m) {
int u, v, t;
scanf("%d%d%d", &u, &v, &t);
if (h[u] >= t) adj[u].push_back({v,t});
if (h[v] >= t) adj[v].push_back({u,t});
}
fill(dist,dist+N,INF);
priority_queue<pair<ll,int>> pq;
dist[1] = 0, pq.push({0,1});
while (not empty(pq)) {
auto [d,a] = pq.top(); pq.pop(), d=-d;
if (dist[a] < d) continue;
for (auto [b,w] : adj[a]) {
if (dist[a] < x) {
if (x-dist[a] >= w) {
dist[b] = max(dist[a]+w,0LL+x-h[b]);
} else {
dist[b] = dist[a]+w-(x-dist[a]);
}
pq.push({-dist[b],b});
}
else if (dist[b] > dist[a]+2*w) {
dist[b] = dist[a]+2*w;
pq.push({-dist[b],b});
}
}
}
if (dist[n] < x) printf("%lld", h[n]-(x-dist[n]) + dist[n]);
else if (h[n]+dist[n] >= INF) puts("-1");
else printf("%lld", h[n]+dist[n]);
}
Compilation message
2014_ho_t4.cpp: In function 'int main()':
2014_ho_t4.cpp:19:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
19 | scanf("%d%d%d", &n, &m, &x);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~
2014_ho_t4.cpp:20:21: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
20 | rep(i,1,n) scanf("%d", &h[i]);
| ~~~~~^~~~~~~~~~~~~
2014_ho_t4.cpp:23:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
23 | scanf("%d%d%d", &u, &v, &t);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3412 KB |
Output is correct |
2 |
Correct |
2 ms |
3412 KB |
Output is correct |
3 |
Incorrect |
3 ms |
3456 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
107 ms |
6748 KB |
Output is correct |
2 |
Correct |
92 ms |
12876 KB |
Output is correct |
3 |
Correct |
70 ms |
8384 KB |
Output is correct |
4 |
Correct |
106 ms |
13076 KB |
Output is correct |
5 |
Correct |
84 ms |
12496 KB |
Output is correct |
6 |
Correct |
5 ms |
3412 KB |
Output is correct |
7 |
Correct |
64 ms |
3800 KB |
Output is correct |
8 |
Correct |
103 ms |
11248 KB |
Output is correct |
9 |
Correct |
70 ms |
4940 KB |
Output is correct |
10 |
Correct |
45 ms |
4048 KB |
Output is correct |
11 |
Correct |
13 ms |
4436 KB |
Output is correct |
12 |
Correct |
63 ms |
7920 KB |
Output is correct |
13 |
Correct |
30 ms |
7764 KB |
Output is correct |
14 |
Correct |
65 ms |
4432 KB |
Output is correct |
15 |
Correct |
4 ms |
3540 KB |
Output is correct |
16 |
Correct |
84 ms |
12480 KB |
Output is correct |
17 |
Correct |
11 ms |
4228 KB |
Output is correct |
18 |
Correct |
12 ms |
4180 KB |
Output is correct |
19 |
Correct |
27 ms |
3596 KB |
Output is correct |
20 |
Correct |
14 ms |
4436 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3412 KB |
Output is correct |
2 |
Correct |
2 ms |
3412 KB |
Output is correct |
3 |
Incorrect |
3 ms |
3456 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |