#include #define rep(i,n) for(int i = 0; i < (n); i++) using namespace std; typedef long long ll; // g <- pair < v , cost > template < class T > vector< T > dijkstra(vector>> &graph, int s) { T INF = numeric_limits< T >::max(); vector dist(graph.size(), INF); priority_queue, vector>, greater>> q; q.push({dist[s] = T(0), s}); while(!q.empty()){ auto [uc, ui] = q.top(); q.pop(); if(uc != dist[ui]) continue; for(auto [vi, vc] : graph[ui]) if(dist[vi] > uc + vc) q.push({dist[vi] = uc + vc, vi}); } return dist; } int main(){ cin.tie(0); ios::sync_with_stdio(0); ll N,M,P,Q,T; cin >> N >> M >> P >> Q >> T; P--, Q--; vector>> G(N); rep(_,M) { int a,b,c; cin >> a >> b >> c; a--, b--; G[a].push_back({b, c}); G[b].push_back({a, c}); } auto dist0 = dijkstra(G, 0); auto distP = dijkstra(G, P); auto distQ = dijkstra(G, Q); ll ans = -1; rep(i,N)rep(j,N) { if(dist0[i] + max(distP[i] + distP[j], distQ[i] + distQ[j]) + dist0[j] <= T) { ans = max(ans, T - max(distP[i] + distP[j], distQ[i] + distQ[j])); } } if(dist0[P] + distP[Q] + distQ[0] <= T) ans = max(ans, T); cout << ans << endl; }