43 lines
1.0 KiB
Swift
43 lines
1.0 KiB
Swift
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()
|
|
}
|
|
}
|
|
|