#include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define repr(i, s, n) for (int i = (s); i < (int)(n); i++) #define revrep(i, n) for (int i = (n) - 1; i >= 0; i--) #define revrepr(i, s, n) for (int i = (n) - 1; i >= s; i--) #define debug(x) cerr << #x << ": " << x << "\n" #define popcnt(x) __builtin_popcount(x) using ll = long long; using P = pair; 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; } template istream& operator >>(istream &is, vector &v) { for (int i = 0; i < (int)v.size(); i++) cin >> v.at(i); return is; } template ostream& operator <<(ostream &os, pair p) { cout << '(' << p.first << ", " << p.second << ')'; return os; } template void print(const vector &v, const string &delimiter) { rep(i, v.size()) cout << (0 < i ? delimiter : "") << v.at(i); cout << endl; } template void print(const vector> &vv, const string &delimiter) { for (const auto &v: vv) print(v, delimiter); } template struct WeightedGraph { struct Edge{ int to; Weight weight; }; int n; vector> edges; Weight zero; Weight inf; WeightedGraph(int n, Weight zero, Weight inf) : n(n), edges(n), zero(zero), inf(inf) {} void add_edge(int u, int v, Weight w) { edges[u].push_back({v, w}); } }; template vector dijkstra(int s, const WeightedGraph &g/*, vector &d*/) { using Node = pair; priority_queue, greater> que; vector d(g.n, g.inf); d[s] = g.zero; que.push(Node(g.zero, s)); while (!que.empty()) { Node p = que.top(); que.pop(); int v = p.second; if (d[v] < p.first) continue; for (int i = 0; i < (int)g.edges[v].size(); i++) { auto e = g.edges[v][i]; if (d[e.to] > d[v] + e.weight) { d[e.to] = d[v] + e.weight; que.push(Node(d[e.to], e.to)); } } } return d; } int main() { int n, m, p, q, t; cin >> n >> m >> p >> q >> t; p--; q--; WeightedGraph graph(n, 0, 1e18); rep(i, m) { int a, b, w; cin >> a >> b >> w; a--; b--; graph.add_edge(a, b, w); graph.add_edge(b, a, w); } auto dist_p_to = dijkstra(p, graph); auto dist_q_to = dijkstra(q, graph); auto dist_0_to = dijkstra(0, graph); if (dist_0_to[p] + dist_p_to[q] + dist_q_to[0] <= t || dist_0_to[q] + dist_q_to[p] + dist_p_to[0] <= t) { cout << t << endl; return 0; } ll ans = -1; rep(v, n) { ll solo = max(dist_p_to[v] * 2, dist_q_to[v] * 2); if (dist_0_to[v] * 2 + solo <= t) { chmax(ans, t - solo); } } cout << ans << endl; }