-
Notifications
You must be signed in to change notification settings - Fork 0
/
Chocolate Journey - HackerEarth - Dijkshtra
80 lines (70 loc) · 1.58 KB
/
Chocolate Journey - HackerEarth - Dijkshtra
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int INF = 100000000000;
vector<pair<int,int>> g[100001];
void dijkstra(int s,int n,vector<int> &d) {
d.assign(n+1,INF);
using pii = pair<int,int>;
priority_queue<pii, vector<pii>, greater<pii>> q;
q.push({0,s});
d[s] = 0;
while (!q.empty()) {
int v = q.top().second;
int d_v = q.top().first;
q.pop();
if(d[v]!=d_v)
continue;
for(auto edge:g[v])
{
int to = edge.first;
int w = edge.second;
if(d[to] > d[v] + w)
{
d[to] = d[v] + w;
q.push({d[to],to});
}
}
}
}
signed main()
{
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int n,m,k,x;
cin >> n >> m >> k >> x;
vector<int> c(n+1,0);
for(int i=0;i<k;i++)
{
int cc;cin >> cc;
c[cc]=1;
}
for(int i=0;i<m;i++)
{
int u,v,w;
cin >> u >> v >> w;
g[u].push_back({v,w});
g[v].push_back({u,w});
}
int A,B;
cin >> A >> B;
vector<int>d(n+1);
dijkstra(B,n,d);
vector<pair<int,int>> range;
for(int i=1;i<=n;i++)
{
if(i!=B && d[i]<=x && c[i])
range.push_back({i,d[i]});
}
dijkstra(A,n,d);
int mini = 100000000000;
for(int i=0;i<(int)range.size();i++)
{
if(d[range[i].first]!=INF)
{
mini = min(mini,d[range[i].first] + range[i].second);
}
}
if(mini == INF)
mini = -1;
cout << mini << '\n';
}