#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
const int MX=100010, inf=2e9;
const ll linf=2e17;
int n, m, h0, H[MX];
vector<pii> G[MX];
ll S[MX];
ll ans=linf;
void gozero(){
ll D[MX];
priority_queue<pli, vector<pli>, greater<pli>> Q;
for(int i=1; i<=n; i++) D[i]=linf;
D[1]=0; Q.push({0,1});
while(!Q.empty()){
int v=Q.top().second; ll d=Q.top().first; Q.pop();
if(D[v]!=d) continue;
for(pii &p:G[v]){
int x,c; tie(x,c)=p;
ll now=d+c;
if(h0-now<0) continue;
if(h0-now>H[x]) now=h0-H[x];
if(now>=D[x]) continue;
D[x]=now; Q.push({now,x});
}
}
if(D[n]<=h0) ans=D[n]+H[n]-(h0-D[n]);
for(int i=1; i<=n; i++) S[i]=linf;
for(int i=1; i<=n; i++){
if(h0<D[i]) continue;
S[i]=min(S[i], D[i] + (h0-D[i]));
for(pii &e:G[i]){
int x,c; tie(x,c)=e;
S[x]=min(S[x], D[i] + abs(h0-D[i]-c) + c);
}
}
if(S[n]<linf) ans=min(ans, S[n]+H[n]);
// for(int i=1; i<=n; i++) cout<<S[i]<<' ';
// cout<<'\n';
}
ll solve(){
ll D[MX];
priority_queue<pli, vector<pli>, greater<pli>> Q;
for(int i=1; i<=n; i++) D[i]=linf;
for(int i=1; i<=n; i++)
if(S[i]<linf) D[i]=S[i], Q.push({D[i], i});
while(!Q.empty()){
int v=Q.top().second; ll d=Q.top().first; Q.pop();
if(D[v]!=d) continue;
for(pii &e:G[v]){
int x,c; tie(x,c)=e; c*=2;
if(D[v]+c>=D[x]) continue;
D[x]=D[v]+c; Q.push({D[x], x});
}
}
// for(int i=1; i<=n; i++) cout<<D[i]<<' ';
// cout<<'\n';
return D[n]+H[n];
}
int main(){
ios::sync_with_stdio(0); cin.tie(0);
cin>>n>>m>>h0;
for(int i=1; i<=n; i++) cin>>H[i];
for(int i=1; i<=m; i++){
int u, v, c; cin>>u>>v>>c;
if(H[u]-c>=0) G[u].push_back({v,c});
if(H[v]-c>=0) G[v].push_back({u,c});
}
gozero();
ans=min(ans, solve());
cout<<(ans>=linf ? -1 : ans);
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
2680 KB |
Output is correct |
2 |
Correct |
6 ms |
2956 KB |
Output is correct |
3 |
Correct |
6 ms |
2960 KB |
Output is correct |
4 |
Correct |
6 ms |
3048 KB |
Output is correct |
5 |
Correct |
7 ms |
3192 KB |
Output is correct |
6 |
Correct |
5 ms |
3192 KB |
Output is correct |
7 |
Correct |
5 ms |
3192 KB |
Output is correct |
8 |
Correct |
6 ms |
3356 KB |
Output is correct |
9 |
Correct |
5 ms |
3356 KB |
Output is correct |
10 |
Correct |
6 ms |
3372 KB |
Output is correct |
11 |
Correct |
4 ms |
3372 KB |
Output is correct |
12 |
Correct |
6 ms |
3416 KB |
Output is correct |
13 |
Correct |
8 ms |
3504 KB |
Output is correct |
14 |
Correct |
6 ms |
3504 KB |
Output is correct |
15 |
Correct |
7 ms |
3532 KB |
Output is correct |
16 |
Correct |
5 ms |
3532 KB |
Output is correct |
17 |
Correct |
5 ms |
3532 KB |
Output is correct |
18 |
Correct |
5 ms |
3532 KB |
Output is correct |
19 |
Correct |
5 ms |
3532 KB |
Output is correct |
20 |
Correct |
5 ms |
3608 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
206 ms |
15176 KB |
Output is correct |
2 |
Correct |
258 ms |
25356 KB |
Output is correct |
3 |
Correct |
212 ms |
25356 KB |
Output is correct |
4 |
Correct |
276 ms |
33336 KB |
Output is correct |
5 |
Correct |
209 ms |
36876 KB |
Output is correct |
6 |
Correct |
12 ms |
36876 KB |
Output is correct |
7 |
Correct |
123 ms |
36876 KB |
Output is correct |
8 |
Correct |
285 ms |
47456 KB |
Output is correct |
9 |
Correct |
191 ms |
47456 KB |
Output is correct |
10 |
Correct |
93 ms |
50036 KB |
Output is correct |
11 |
Correct |
35 ms |
50036 KB |
Output is correct |
12 |
Correct |
136 ms |
56988 KB |
Output is correct |
13 |
Correct |
73 ms |
58120 KB |
Output is correct |
14 |
Correct |
167 ms |
61468 KB |
Output is correct |
15 |
Correct |
10 ms |
61468 KB |
Output is correct |
16 |
Correct |
211 ms |
72448 KB |
Output is correct |
17 |
Correct |
25 ms |
72448 KB |
Output is correct |
18 |
Correct |
28 ms |
72448 KB |
Output is correct |
19 |
Correct |
68 ms |
72448 KB |
Output is correct |
20 |
Correct |
38 ms |
72448 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
2680 KB |
Output is correct |
2 |
Correct |
6 ms |
2956 KB |
Output is correct |
3 |
Correct |
6 ms |
2960 KB |
Output is correct |
4 |
Correct |
6 ms |
3048 KB |
Output is correct |
5 |
Correct |
7 ms |
3192 KB |
Output is correct |
6 |
Correct |
5 ms |
3192 KB |
Output is correct |
7 |
Correct |
5 ms |
3192 KB |
Output is correct |
8 |
Correct |
6 ms |
3356 KB |
Output is correct |
9 |
Correct |
5 ms |
3356 KB |
Output is correct |
10 |
Correct |
6 ms |
3372 KB |
Output is correct |
11 |
Correct |
4 ms |
3372 KB |
Output is correct |
12 |
Correct |
6 ms |
3416 KB |
Output is correct |
13 |
Correct |
8 ms |
3504 KB |
Output is correct |
14 |
Correct |
6 ms |
3504 KB |
Output is correct |
15 |
Correct |
7 ms |
3532 KB |
Output is correct |
16 |
Correct |
5 ms |
3532 KB |
Output is correct |
17 |
Correct |
5 ms |
3532 KB |
Output is correct |
18 |
Correct |
5 ms |
3532 KB |
Output is correct |
19 |
Correct |
5 ms |
3532 KB |
Output is correct |
20 |
Correct |
5 ms |
3608 KB |
Output is correct |
21 |
Correct |
206 ms |
15176 KB |
Output is correct |
22 |
Correct |
258 ms |
25356 KB |
Output is correct |
23 |
Correct |
212 ms |
25356 KB |
Output is correct |
24 |
Correct |
276 ms |
33336 KB |
Output is correct |
25 |
Correct |
209 ms |
36876 KB |
Output is correct |
26 |
Correct |
12 ms |
36876 KB |
Output is correct |
27 |
Correct |
123 ms |
36876 KB |
Output is correct |
28 |
Correct |
285 ms |
47456 KB |
Output is correct |
29 |
Correct |
191 ms |
47456 KB |
Output is correct |
30 |
Correct |
93 ms |
50036 KB |
Output is correct |
31 |
Correct |
35 ms |
50036 KB |
Output is correct |
32 |
Correct |
136 ms |
56988 KB |
Output is correct |
33 |
Correct |
73 ms |
58120 KB |
Output is correct |
34 |
Correct |
167 ms |
61468 KB |
Output is correct |
35 |
Correct |
10 ms |
61468 KB |
Output is correct |
36 |
Correct |
211 ms |
72448 KB |
Output is correct |
37 |
Correct |
25 ms |
72448 KB |
Output is correct |
38 |
Correct |
28 ms |
72448 KB |
Output is correct |
39 |
Correct |
68 ms |
72448 KB |
Output is correct |
40 |
Correct |
38 ms |
72448 KB |
Output is correct |
41 |
Correct |
132 ms |
74004 KB |
Output is correct |
42 |
Correct |
61 ms |
76928 KB |
Output is correct |
43 |
Correct |
144 ms |
79972 KB |
Output is correct |
44 |
Correct |
162 ms |
86536 KB |
Output is correct |
45 |
Correct |
246 ms |
94500 KB |
Output is correct |
46 |
Correct |
28 ms |
94500 KB |
Output is correct |
47 |
Correct |
420 ms |
102160 KB |
Output is correct |
48 |
Correct |
439 ms |
105244 KB |
Output is correct |
49 |
Correct |
278 ms |
107156 KB |
Output is correct |
50 |
Correct |
382 ms |
113040 KB |
Output is correct |
51 |
Correct |
58 ms |
113040 KB |
Output is correct |
52 |
Correct |
285 ms |
114932 KB |
Output is correct |
53 |
Correct |
170 ms |
116320 KB |
Output is correct |
54 |
Correct |
167 ms |
122820 KB |
Output is correct |
55 |
Correct |
183 ms |
127348 KB |
Output is correct |
56 |
Correct |
339 ms |
139452 KB |
Output is correct |
57 |
Correct |
66 ms |
139452 KB |
Output is correct |
58 |
Correct |
10 ms |
139452 KB |
Output is correct |
59 |
Correct |
130 ms |
139452 KB |
Output is correct |
60 |
Correct |
81 ms |
140728 KB |
Output is correct |