4
7
1 2 4
1 4 6
2 1 3
2 3 7
3 1 5
3 4 4
4 3 2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
import java.io.BufferedReader
import java.io.BufferedWriter
import java.io.InputStreamReader
import java.io.OutputStreamWriter
import java.lang.Integer.min
import java.util.*
val br = BufferedReader(InputStreamReader(System.`in`))
val bw = BufferedWriter(OutputStreamWriter(System.out))
val n = readLine()!!.toInt()
val m = readLine()!!.toInt()
val graph = Array(n+1,{IntArray(n+1,{1e9.toInt()})})
fun main()=with(br){
for(i in 1 until n+1){
for(j in 1 until n+1){
if(i==j){
graph[i][j]=0
}
}
}
for(i in 0 until m){
var(a,b,c)=readLine()!!.split(" ").map{it.toInt()}
graph[a][b]=c
}
for(i in 1 until n+1){
for(j in 1 until n+1){
for(k in 1 until n+1){
graph[j][k]=min(graph[j][k],graph[j][i]+graph[i][k])
}
}
}
for(i in 1 .. n){
for(j in 1 .. n) {
if (graph[i][j] == 1e9.toInt()) {
print("INFINITY ")
} else {
print("${graph[i][j]} ")
}
}
println()
}
bw.flush()
bw.close()
}
|
cs |
'알고리즘 공부 > 미분류' 카테고리의 다른 글
프로그래머스 멀쩡한 사각형 (0) | 2022.01.11 |
---|---|
백준 1730번 파이썬 (0) | 2022.01.06 |
백준 1752번 with Kotlin (0) | 2021.08.20 |
백준 1027번 with Kotlin (0) | 2021.08.20 |
최단경로 다익스트라 알고리즘 (0) | 2021.08.10 |