import java.util.PriorityQueue import java.util.Scanner import java.io.BufferedReader import java.io.InputStreamReader data class Edge(val v: Int, val w: Long) val INF = 1e18.toLong() fun dijkstra(g: Array>, s: Int): Array { data class State(val d: Long, val u: Int) : Comparable { override operator fun compareTo(other: State): Int { return d.compareTo(other.d) } } var q: PriorityQueue = PriorityQueue() val n = g.size var dist = Array(n, {INF}) dist[s] = 0 q.add(State(0, s)) while (!q.isEmpty()) { val (d, u) = q.remove() if (d > dist[u]) continue for (e in g[u]) { if (dist[e.v] > dist[u] + e.w) { dist[e.v] = dist[u] + e.w q.add(State(dist[e.v], e.v)) } } } return dist } class Reader { var buf: ByteArray = ByteArray(5000000) var k: Int = 0 var n: Int = 0 init { check() } private fun check() { if (k == n) { n = System.`in`.read(buf) k = 0 } } private fun peek(): Byte { return buf[k] } private fun next() { k++ check() } fun long(): Long { var ans: Long = 0 while ('0'.toByte() > peek()) next() while ('0'.toByte() <= peek()) { ans = ans * 10 + (peek() - '0'.toLong()) next() } return ans } fun int(): Int { return long().toInt() } } fun main(args: Array) { var r = Reader() val N = r.int() val M = r.int() val P = r.int() - 1 val Q = r.int() - 1 val T = r.long() var g = Array(N, {mutableListOf()}) repeat(M) { val u = r.int() - 1 val v = r.int() - 1 val w = r.long() g[u].add(Edge(v, w)) g[v].add(Edge(u, w)) } val start = System.currentTimeMillis() val d0 = dijkstra(g, 0) val dp = dijkstra(g, P) val dq = dijkstra(g, Q) val end = System.currentTimeMillis() var ans = -INF if (dp[0] + dp[Q] + dq[0] <= T) { ans = T } for (i in 0..N-1) { for (j in 0..N-1) { if (d0[i] + d0[j] + maxOf(dp[i] + dp[j], dq[i] + dq[j]) <= T) { ans = maxOf(ans, T - maxOf(dp[i] + dp[j], dq[i] + dq[j])) } } } System.err.println(end - start) if (ans < 0L) { println(-1) } else { println(ans) } }