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;
#define fileio() freopen("input.txt","r",stdin); freopen("output.txt","w",stdout)
#define fio() ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define all(x) (x).begin(), (x).end()
#define allr(x) x.rbegin(), x.rend()
#define cmprs(x) sort(all(x)),x.erase(unique(all(x)),x.end())
#define endl "\n"
#define sp " "
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define F first
#define S second
#define rz resize
#define sz(a) (int)(a.size())
#define clr(a) a.clear()
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<int, ll> pil;
typedef tuple<int, int, int> tpi;
typedef tuple<ll, ll, ll> tpl;
typedef pair<double, ll> pdl;
typedef pair<double, int> pdi;
const int dx[] = {1,-1,0,0,1,1,-1,-1};
const int dy[] = {0,0,1,-1,1,-1,1,-1};
const ll MOD = 1e9+7;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
const int MAX = 101010; // PLZ CHK!
int N,M;
ll X,H[MAX],D[MAX],C[MAX];
vector<pll> G[MAX];
typedef array<ll,3> arr;
int main() {
fio();
cin>>N>>M>>X;
for (int i=1; i<=N; i++) cin>>H[i];
for (int i=0; i<M; i++) {
int u,v,w; cin>>u>>v>>w;
if (w<=H[u]) G[u].pb({v,w});
if (w<=H[v]) G[v].pb({u,w});
}
fill(D,D+N+10,LINF);
priority_queue<arr,vector<arr>,greater<arr>> pq;
D[1]=0; C[1]=X;
pq.push({D[1],1,C[1]});
while (!pq.empty()) {
auto [dst,cur,cht]=pq.top(); pq.pop();
if (dst!=D[cur]) continue;
for (auto [nxt,cst]:G[cur]) {
ll nht=cht-cst,cst2=0;
if (C[cur]<cst) {
cst2+=cst-C[cur];
nht=0;
}
if (C[cur]-cst>H[nxt]) {
cst2+=C[cur]-(H[nxt]+cst);
nht=H[nxt];
}
if (D[nxt]>D[cur]+cst+cst2) {
D[nxt]=D[cur]+cst+cst2;
C[nxt]=nht;
pq.push({D[nxt],nxt,C[nxt]});
}
}
}
// for (int i=1; i<=N; i++) cout<<i<<sp<<D[i]<<sp<<C[i]<<endl;
cout<<(D[N]==LINF?-1:D[N]+H[N]-C[N]);
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... |