This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;
inline void fastio() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
}
const ll INF = 1e18;
int main() {
fastio();
ll n, m, x;
cin >> n >> m >> x;
vector<ll> h(n);
vector<vector<pll>> gr(n);
for (auto& i : h) {
cin >> i;
}
for (ll i = 0, a, b, c; i < m; i++) {
cin >> a >> b >> c;
if (h[a - 1] >= c) gr[a - 1].emplace_back(b - 1, c);
if (h[b - 1] >= c) gr[b - 1].emplace_back(a - 1, c);
}
priority_queue<pll, vector<pll>, greater<pll>> pq;
vector<ll> d(n, INF);
pq.emplace(0, 0);
d[0] = 0;
while (!pq.empty()) {
auto [dist, cur] = pq.top();
pq.pop();
if (dist > d[cur]) continue;
ll c = max(0LL, x - d[cur]), tmp;
for (auto& [nxt, cost] : gr[cur]) {
if (cost < c - h[nxt]) {
tmp = c - h[nxt];
} else if (c - h[nxt] <= cost and cost <= c) {
tmp = cost;
} else {
tmp = 2 * cost - c;
}
if (d[nxt] > d[cur] + tmp) {
d[nxt] = d[cur] + tmp;
pq.emplace(d[nxt], nxt);
}
}
}
ll ans = d.back() + h.back() + min(0LL, d.back() - x);
cout << (ans < INF ? ans : -1);
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |