제출 #1343975

#제출 시각아이디문제언어결과실행 시간메모리
1343975biank날다람쥐 (JOI14_ho_t4)C++20
100 / 100
620 ms22944 KiB
#include <bits/stdc++.h>
 
using namespace std;
 
#define forsn(i, s, n) for (int i = int(s); i < int(n); i++)
#define forn(i, n) forsn(i, 0, n)
#define dforsn(i, s, n) for (int i = int(n) - 1; i >= int(s); i--)
#define dforn(i, n) dforsn(i, 0, n)
#define qforn(i, n) for (i = 0; i < n; i++)
 
using vi = vector<int>;
using ii = pair<int, int>;
using vii = vector<ii>;
using ll = long long;
using ld = long double;
using vll = vector<ll>;
using vb = vector<bool>;
using pll = pair<ll, ll>;

#define sz(x) int(x.size())
#define all(x) begin(x), end(x)

#define pb push_back
#define eb emplace_back

#define fst first
#define snd second

const ll INF = 1e18;

int main() {
    ios::sync_with_stdio(0);                
    cin.tie(0); cout.tie(0);
    
    int n, m, x;
    cin >> n >> m >> x;
    
    vi max_h(n);
    forn(i, n) cin >> max_h[i];
    
    vector<vii> adj(n);
    forn(i, m) {
        int a, b, t;
        cin >> a >> b >> t;
        --a, --b;
        adj[a].eb(b, t);
        adj[b].eb(a, t);
    }
    
    map<pair<int, int>, ll> dist;
    priority_queue<pair<ll, ii>, vector<pair<ll, ii>>, greater<pair<ll, ii>>> pq;
    pq.emplace(dist[{0, x}] = 0, make_pair(0, x));
    set<int> vis;
    while (!pq.empty()) {
        auto [d, pos] = pq.top();
        pq.pop(); 
        auto [u, h] = pos;
        if (dist[pos] != d) continue;
        if (!vis.insert(u).snd) continue;
        for (auto [v, t] : adj[u]) if (!vis.count(v)) {
            if (h - t < 0) {
                if (t > max_h[u]) continue;
                ll w = 2 * t - h; 
                if (!dist.count({v, 0}) || dist[{v, 0}] > d + w) {
                    pq.emplace(dist[{v, 0}] = d + w, make_pair(v, 0));
                }
            } else if (h - t > max_h[v]) {
                ll w = h - max_h[v];
                if (!dist.count({v, max_h[v]}) || dist[{v, max_h[v]}] > d + w) {
                    pq.emplace(dist[{v, max_h[v]}] = d + w, make_pair(v, max_h[v]));
                }
            } else {
                ll w = t;
                if (!dist.count({v, h - t}) || dist[{v, h - t}] > d + w) {
                    pq.emplace(dist[{v, h - t}] = d + w, make_pair(v, h - t));
                }
            }
        }
    }
    
    ll ret = INF;
    for (auto [pos, d] : dist) {
        auto [u, h] = pos;
        if (u == n - 1) ret = min(ret, d + max_h[n - 1] - h);
    }
    
    if (ret == INF) cout << "-1\n";
    else cout << ret << "\n";
    
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...