Submission #991144

#TimeUsernameProblemLanguageResultExecution timeMemory
991144gmroh06날다람쥐 (JOI14_ho_t4)C++17
100 / 100
100 ms23504 KiB
#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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...