20251004 baekjoon
This commit is contained in:
parent
d4d8c22ef2
commit
6d3192d0ae
42
code_study/Baekjoon/swift/11404.swift
Normal file
42
code_study/Baekjoon/swift/11404.swift
Normal file
@ -0,0 +1,42 @@
|
||||
let inf = 100000001
|
||||
|
||||
if let n = Int(readLine() ?? ""), let m = Int(readLine() ?? "") {
|
||||
var cost: [[Int]] = Array(repeating: Array(repeating: inf, count: n+1), count: n+1)
|
||||
|
||||
for i in 1...n {
|
||||
cost[i][i] = 0
|
||||
}
|
||||
|
||||
for _ in 0..<m {
|
||||
if let input = readLine(),
|
||||
let busInfo = input.split(separator: " ").compactMap({Int($0)}) as? [Int],
|
||||
busInfo.count == 3
|
||||
{
|
||||
let a = busInfo[0]
|
||||
let b = busInfo[1]
|
||||
let c = busInfo[2]
|
||||
|
||||
cost[a][b] = min(cost[a][b], c)
|
||||
}
|
||||
}
|
||||
|
||||
// Floyd-Warshall Algorithm s -> t -> e
|
||||
for t in 1...n {
|
||||
for s in 1...n {
|
||||
for e in 1...n {
|
||||
cost[s][e] = min(cost[s][e], cost[s][t] + cost[t][e])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for i in 1...n {
|
||||
for j in 1...n {
|
||||
if cost[i][j] == inf {
|
||||
cost[i][j] = 0
|
||||
}
|
||||
print(Int(cost[i][j]), terminator: " ")
|
||||
}
|
||||
print()
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user