# 【题解】LOJ2759. 「JOI 2014 Final」飞天鼠(最短路)

## 【题解】LOJ2759. 「JOI 2014 Final」飞天鼠(最短路)

//@winlere
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>

using namespace std;  typedef long long ll;
inline int qr(){
register int ret=0,f=0;
register char c=getchar();
while(c<48||c>57)f|=c==45,c=getchar();
while(c>=48&&c<=57) ret=ret*10+c-48,c=getchar();
return f?-ret:ret;
}

const int maxn=1e5+5;
const ll inf=1e18;
typedef pair<ll,int> P;
typedef priority_queue<P,vector<P>,greater<P> > Qp;

Qp q;
ll d[maxn];
int Height[maxn];
pair<int,pair<int,int> > last[maxn];
vector< pair<int,int> > e[maxn];
int h[maxn];
int n,m,X;

inline void add(const int&fr,const int&to,const int&w){
e[fr].push_back({to,w});
}

inline bool dij(){
Qp().swap(q);
for(int t=1;t<=n;++t) d[t]=inf,Height[t]=0;
d[1]=0; Height[1]=X; q.push({0,1});
while(q.size()){
auto now=q.top();
int Cur=now.second,H=Height[Cur];
q.pop();
if(now.first>d[Cur]) continue;
for(auto t:e[Cur]){
int to=t.first;
ll len=t.second;
if(H-len>h[to]){
ll g=H-h[to]+d[Cur];
if(d[to]>g){
d[to]=g;
Height[to]=h[to];
q.push({d[to],to});
}
continue;
}
if(H-len<0){
ll g=d[Cur]+len-H+len;
if(d[to]>g){
d[to]=g;
Height[to]=0;
q.push({d[to],to});
}
continue;
}
if(d[to]>d[Cur]+len){
d[to]=d[Cur]+len;
Height[to]=H-len;
q.push({d[to],to});
continue;
}
}
}
if(d[n]==inf) return 0;
return 1;
}

int main(){
n=qr(); m=qr(); X=qr();
for(int t=1;t<=n;++t) h[t]=qr();
for(int t=1;t<=m;++t){
int t1=qr(),t2=qr(),t3=qr();