#include #include #include #include #include #include #include using namespace std; typedef long long LL; typedef pair PII; typedef pair PLI; const int N = 2010; const LL INF = 0x3f3f3f3f3f3f3f3fLL; vector g[N]; priority_queue, greater> pq; LL d1[N], dp[N], dq[N], ans; int n, m, p, q, t; void Dijkstra(int bg, LL dist[]) { fill(dist + 1, dist + n + 1, INF); dist[bg] = 0LL; pq.push({ 0LL, bg }); while (!pq.empty()) { auto [dis, u] = pq.top(); pq.pop(); if (dis != dist[u]) continue; for (auto [v, w] : g[u]) { if (dist[v] <= dis + w) continue; dist[v] = dis + w; pq.push({ dist[v], v }); } } } int main() { // freopen("friend.in", "r", stdin); // freopen("friend.out", "w", stdout); scanf("%d%d%d%d%d", &n, &m, &p, &q, &t); for (int i = 1, a, b, c; i <= m; ++i) { scanf("%d%d%d", &a, &b, &c); g[a].push_back({ b, c }); g[b].push_back({ a, c }); } Dijkstra(1, d1); Dijkstra(p, dp); Dijkstra(q, dq); if (d1[p] + dp[q] + d1[q] <= t) { printf("%d\n", t); return 0; } ans = -1LL; for (int x = 1; x <= n; ++x) { for (int y = x; y <= n; ++y) { LL sep = max(dp[x] + dp[y], dq[x] + dq[y]); if (d1[x] + sep + d1[y] <= t) ans = max(ans, 1LL * t - sep); } } printf("%lld\n", ans); return 0; }