본문 바로가기
코딩연습

[백준] 플로이드 (11404)(Kotlin)

by 유줘니 2020. 1. 30.

원본 문제 : https://www.acmicpc.net/problem/11404

문제 참고 : https://pangsblog.tistory.com/90

 

<첫번째>

import java.io.*
import java.util.*

val INF: Int = 100000
var graph: Array<IntArray> = arrayOf()
var cityCount = 0

fun main() = with(BufferedReader(InputStreamReader(System.`in`))) {

    cityCount = readLine().toInt()
    var busCount = readLine().toInt()

    graph = Array(cityCount + 1) { IntArray( cityCount + 1 ) }

    for ( i in 1..cityCount ) {
        for ( j in 1..cityCount ) {
            if (i==j) continue
            graph[i][j] = INF
        }
    }

    while ( busCount-- > 0 ) {

        val str = readLine().split(" ")

        val start = str[0].toInt()
        val end = str[1].toInt()
        val cost = str[2].toInt()

        graph[start][end] = Math.min(graph[start][end], cost)

    }

    floydWarshall()
    print()

}

fun floydWarshall() {

    for ( k in 1..cityCount ) {
        for ( i in 1..cityCount ) {
            for ( j in  1..cityCount ) {
                graph[i][j] = Math.min(graph[i][k] + graph[k][j], graph[i][j])
            }
        }
    }
}

fun print() {

    var str = ""

    for ( i in 1..cityCount) {
        for ( j in 1..cityCount ) {
            if ( graph[i][j] >= INF ) str += "0 "
            else str += "${graph[i][j]} "
        }
        str += "\n"
    }
    println(str)
}

'코딩연습' 카테고리의 다른 글

[백준] 경로 찾기 (11403)(Kotlin)  (0) 2020.01.30
[배준] 바이러스 (2606)(Kotlin)  (0) 2020.01.30
[백준] 숨바꼭질 (1697)(Kotlin)  (0) 2020.01.29
[백준] 토마토 (7576)(Kotlin)  (0) 2020.01.29
[백준] DFS와 BFS (1260)(Kotlin)  (0) 2020.01.29

댓글