#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include template inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } constexpr long long MAX = 5100000; constexpr long long INF = 1LL << 60; constexpr int inf = 1 << 28; constexpr long long mod = 1000000007LL; //constexpr long long mod = 998244353LL; using namespace std; typedef unsigned long long ull; typedef long long ll; void dijkstra(ll start, vector>>& graph, vector& dist) { dist[start] = 0; priority_queue, vector>, greater>> pq; vector used(dist.size(), false); pq.push(make_pair(0, start)); while (!pq.empty()) { ll d, node; tie(d, node) = pq.top(); pq.pop(); if (used[node]) continue; used[node] = true; for (pair element : graph[node]) { ll new_d, new_node; tie(new_node, new_d) = element; new_d += d; if (new_d < dist[new_node]) { dist[new_node] = new_d; pq.push(make_pair(dist[new_node], new_node)); } } } } int main() { /* cin.tie(nullptr); ios::sync_with_stdio(false); */ ll N, M, P, Q, T; scanf("%lld %lld %lld %lld %lld", &N, &M, &P, &Q, &T); P--; Q--; vector>> g(N); for (int i = 0; i < M; i++) { ll a, b, c; scanf("%lld %lld %lld", &a, &b, &c); a--; b--; g[a].emplace_back(b, c); g[b].emplace_back(a, c); } vector dist1(N, INF), dist2(N, INF), dist3(N, INF); dijkstra(0, g, dist1); dijkstra(P, g, dist2); dijkstra(Q, g, dist3); if (dist1[P] + dist2[Q] + dist1[Q] <= T) { cout << T << endl; return 0; } if (max(dist1[P] * 2, dist1[Q] * 2) > T) { puts("-1"); return 0; } ll res = 0; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { ll t1 = dist1[i] + dist2[i] + dist2[j] + dist1[j]; ll t2 = dist1[i] + dist3[i] + dist3[j] + dist1[j]; if (max(t1, t2) > T) continue; chmax(res, T - max(dist2[i]+dist2[j],dist3[i]+dist3[j])); } } cout << res << endl; return 0; /* おまじないを使ったらscanfとprintf関連注意!!!!!!!!!!!! */ }