Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- fun main() {
- val (n, sIn, fIn) = readInts()
- val s = sIn - 1; val f = fIn - 1
- val inf = Int.MAX_VALUE / 2
- val matrix = List(n) { readInts().map { if (it == -1) inf else it } }
- val mark = BooleanArray(n) // IntArray заполн. 0, BooleanArray — false
- val dist = IntArray(n) { inf }.also { it[s] = 0 }
- while (true) {
- val v = matrix.indices.filter { !mark[it] }.minByOrNull { dist[it] }
- ?.takeIf { dist[it] < inf } ?: break
- mark[v] = true
- for (u in matrix.indices) {
- if (dist[u] > dist[v] + matrix[v][u]) {
- dist[u] = dist[v] + matrix[v][u]
- }
- }
- }
- println(if (dist[f] < inf) dist[f] else -1)
- }
- fun readLn(): String {
- while (true) {
- val s = readLine()!!.trim()
- if (s.isNotEmpty()) return s
- }
- }
- fun readStrings() = readLn().split(" ")
- fun readInts() = readStrings().map { it.toInt() }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement