#include #include #include #include #include #define _USE_MATH_DEFINES #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #define FOR(i,m,n) for(int i=(m);i<(n);++i) #define REP(i,n) FOR(i,0,n) #define ALL(v) (v).begin(),(v).end() const int INF = 0x3f3f3f3f; const long long LINF = 0x3f3f3f3f3f3f3f3fLL; const double EPS = 1e-8; const int MOD = 1000000007; // 998244353; const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1}; /*-------------------------------------------------*/ using CostType = long long; struct Edge { int src, dst; CostType cost; Edge(int src, int dst, CostType cost = 0) : src(src), dst(dst), cost(cost) {} inline bool operator<(const Edge &rhs) const { return cost != rhs.cost ? cost < rhs.cost : dst != rhs.dst ? dst < rhs.dst : src < rhs.src; } inline bool operator<=(const Edge &rhs) const { return cost <= rhs.cost; } inline bool operator>(const Edge &rhs) const { return cost != rhs.cost ? cost > rhs.cost : dst != rhs.dst ? dst > rhs.dst : src > rhs.src; } inline bool operator>=(const Edge &rhs) const { return cost >= rhs.cost; } }; struct Dijkstra { using Pci = pair; Dijkstra(const vector > &graph, const CostType CINF = LINF) : graph(graph), CINF(CINF) {} vector build(int s) { int n = graph.size(); vector dist(n, CINF); dist[s] = 0; prev.assign(n, -1); priority_queue, greater > que; que.emplace(0, s); while (!que.empty()) { Pci pr = que.top(); que.pop(); int ver = pr.second; if (dist[ver] < pr.first) continue; for (Edge e : graph[ver]) { if (dist[e.dst] > dist[ver] + e.cost) { dist[e.dst] = dist[ver] + e.cost; prev[e.dst] = ver; que.emplace(dist[e.dst], e.dst); } } } return dist; } vector build_path(int t) { vector res; for (; t != -1; t = prev[t]) res.emplace_back(t); reverse(ALL(res)); return res; } private: vector > graph; const CostType CINF; vector prev; }; int main() { cin.tie(0); ios::sync_with_stdio(false); // freopen("input.txt", "r", stdin); int n, m, p, q, t; cin >> n >> m >> p >> q >> t; --p; --q; vector > graph(n); while (m--) { int a, b, c; cin >> a >> b >> c; --a; --b; graph[a].emplace_back(Edge(a, b, c)); graph[b].emplace_back(Edge(b, a, c)); } Dijkstra dij(graph); vector > dist(n); REP(i, n) dist[i] = dij.build(i); if (dist[0][p] * 2 > t || dist[0][q] * 2 > t) { cout << -1 << '\n'; return 0; } if (dist[0][p] + dist[p][q] + dist[q][0] <= t) { cout << t << '\n'; return 0; } long long ans = 0; REP(i, n) { long long with = dist[0][i], sad = max(dist[i][p], dist[i][q]); if ((with + sad) * 2 > t) continue; ans = max(ans, t - sad * 2); } cout << ans << '\n'; return 0; }