#include "cyberland.h"
#include <bits/stdc++.h>
using namespace std;
int h2;
vector<bool> vab(1e5), vab2(1e5);
vector<vector<bool>> vis(1e5, vector<bool> (56));
vector<vector<pair<int, double>>> adj(1e5);
vector<vector<double>> dist(1e5, vector<double> (56, 1e18));
void dfs(int u){
vab[u] = 1;
for (auto z: adj[u]){
if (!vab[z.first]) dfs(z.first);
}
}
void dfs2(int u){
vab2[u] = 1;
for (auto z: adj[u]){
if (z.first != h2 && !vab2[z.first]) dfs2(z.first);
}
}
double solve(int n, int m, int k, int h, vector<int> x, vector<int> y, vector<int> c, vector<int> arr){
h2 = h;
if (k > 55) k = 55;
for (int i = 0; i < n; i++){
vab[i] = 0;
vab2[i] = 0;
adj[i].clear();
for (int j = 0; j <= k; j++){
vis[i][j] = 0;
dist[i][j] = 1e18;
}
}
for (int i = 0; i < m; i++){
adj[x[i]].push_back({y[i], c[i]});
adj[y[i]].push_back({x[i], c[i]});
}
dfs(0);
dfs2(0);
if (vab[h] == 0) return -1;
priority_queue<pair<pair<double, bool>, pair<int, int>>, vector<pair<pair<double, bool>, pair<int, int>>>, greater<pair<pair<double, bool>, pair<int, int>>>> pq;
pq.push({{0, 1}, {0, 0}});
for (int i = 1; i < n; i++){
if (arr[i] == 0 && vab2[i] == 1) pq.push({{0, 0}, {i, 1}});
}
while (pq.size()){
double d = pq.top().first.first;
bool b = pq.top().first.second;
int in = pq.top().second.first, l = pq.top().second.second;
pq.pop();
if (vis[in][l]) continue;
vis[in][l] = 1;
dist[in][l] = d;
if (in == h) continue;
for (auto z: adj[in]){
pq.push({{d + z.second, 0}, {z.first, l}});
}
if (arr[in] != 2 || b) continue;
if (l < k) pq.push({{d / 2, 1}, {in, l + 1}});
}
double ans = 1e18;
for (int i = 0; i <= k; i++){
ans = min(ans, dist[h][i]);
}
return ans;
}