/** * @FileName a.cpp * @Author kanpurin * @Created 2020.06.14 19:54:32 **/ #include "bits/stdc++.h" using namespace std; typedef long long ll; template < typename T > struct Dijkstra { private: int V; struct edge { int to; T cost; }; vector< vector< edge > > G; public: const T inf = numeric_limits< T >::max(); vector< T > d; Dijkstra(int V) : V(V) { G.resize(V); } void add_edge(int from, int to, T weight, bool directed = false) { G[from].push_back({to, weight}); if (!directed) G[to].push_back({from, weight}); } void build(int s) { d.assign(V, inf); typedef tuple< T, int > P; priority_queue< P, vector< P >, greater< P > > pq; d[s] = 0; pq.push(P(d[s], s)); while (!pq.empty()) { P p = pq.top(); pq.pop(); int v = get< 1 >(p); if (d[v] < get< 0 >(p)) continue; for (const edge &e : G[v]) { if (d[e.to] > d[v] + e.cost) { d[e.to] = d[v] + e.cost; pq.push(P(d[e.to], e.to)); } } } } }; int main() { int n, m, p, q, t; cin >> n >> m >> p >> q >> t; p--; q--; Dijkstra< ll > g1(n), gp(n), gq(n); for (int i = 0; i < m; i++) { int u, v, c; cin >> u >> v >> c; u--; v--; g1.add_edge(u, v, c); gp.add_edge(u, v, c); gq.add_edge(u, v, c); } g1.build(0); gp.build(p); gq.build(q); if (g1.d[p] + gp.d[q] + gq.d[0] <= t) { cout << t << endl; return 0; } ll ans = -1; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { ll t11 = g1.d[i]; ll tp1 = gp.d[i]; ll tq1 = gq.d[i]; ll t12 = g1.d[j]; ll tp2 = gp.d[j]; ll tq2 = gq.d[j]; if (t11 + tp1 + tp2 + t12 > t) continue; if (t11 + tq1 + tq2 + t12 > t) continue; ans = max(ans,t - max(tp1 + tp2,tq1 + tq2)); } } cout << ans << endl; return 0; }