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;
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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |