#include "cyberland.h"
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define all(x) x.begin(), x.end()
#define pb push_back
#define in(v) for(auto &elem:v){ cin>>elem; }
#define out(v) for(auto elem:v){ cout<<elem<<" "; } cout<<endl;
#define FAST ios_base::sync_with_stdio(false); cin.tie(NULL);
struct Node{
double d;
int u, op;
bool operator<(const Node& other) const{
return d > other.d;
}
};
double solve(int n, int m, int k, int h, vector<int> x, vector<int> y, vector<int> cc, vector<int> arr) {
k = min(k, 67);
vector<vector<pair<int, int>>> adj(n);
for(int i = 0; i<m; i++){
int u = x[i], v = y[i], c = cc[i];
adj[u].pb({c, v});
adj[v].pb({c, u});
}
const ll inf = 4e18;
//out(vis)
vector<bool> vis(n, 0);
queue<int> q;
q.push(0);
vis[0] = 1;
while(!q.empty()){
int u = q.front();
q.pop();
if(u == h) continue;
for(auto [t, v] : adj[u]){
if(v == h) continue;
if(!vis[v]){
vis[v] = 1;
q.push(v);
}
}
}
vector<double> dist(n * (k+1), inf);
priority_queue<Node> pq;
pq.push({0, 0, 0});
dist[0] = 0;
double ans = inf;
for(int i = 0; i<n; i++){
if (arr[i] == 0 && vis[i]){
dist[i*(k+1)+0] = 0;
pq.push({0, i, 0});
}
}
while(!pq.empty()){
auto [c, u, op] = pq.top();
pq.pop();
if(c > dist[u*(k+1)+op]){
continue;
}
if(u == h){
ans = min(c, ans);
continue;
}
for(auto [w, v] : adj[u]){
double ch1 = c + w;
if(ch1 < dist[v*(k+1)+op]){
dist[v*(k+1)+op] = ch1;
pq.push({ch1, v, op});
}
if(op < k && arr[u] == 2){
double ch2 = c / 2 + w;
if(ch2 < dist[v*(k+1)+op+1]){
dist[v*(k+1)+op+1] = ch2;
pq.push({ch2, v, op + 1});
}
}
}
}
if(ans >= 3e18){
return -1;
}
return ans;
}
/*int main(){
int n, m, k, h;
cin>>n>>m>>k>>h;
vector<int> x(m), y(m), c(m), arr(n);
for(ll i = 0; i<m; i++){
cin>>x[i]>>y[i]>>c[i];
}
for(ll i = 0; i<n; i++){
cin>>arr[i];
}
cout<<solve(n, m, k, h, x, y, c, arr);
}*/