Skip to content

Commit 49ccdfa

Browse files
Merge pull request #132 from HmmCorn/liv
플로이드
2 parents aed406e + f626b5a commit 49ccdfa

1 file changed

Lines changed: 27 additions & 0 deletions

File tree

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// 백준 - 11404 플로이드
2+
3+
let cityCount = Int(readLine()!)!
4+
let busCount = Int(readLine()!)!
5+
let basic = [Int](repeating: .max, count: cityCount+1)
6+
var graph = [[Int]](repeating: basic, count: cityCount+1)
7+
8+
for _ in 1...busCount {
9+
let line = readLine()!.split(separator: " ").compactMap { Int($0) }
10+
graph[line[0]][line[1]] = min(graph[line[0]][line[1]], line[2])
11+
}
12+
13+
for city in 1...cityCount {
14+
graph[city][city] = 0
15+
for start in 1...cityCount where graph[start][city] != Int.max {
16+
for end in 1...cityCount where graph[city][end] != Int.max {
17+
let newWeight = graph[start][city] + graph[city][end]
18+
if graph[start][end] > newWeight { graph[start][end] = newWeight }
19+
}
20+
}
21+
}
22+
23+
for i in 1...cityCount {
24+
print((1...cityCount).map {
25+
graph[i][$0] == Int.max ? "0" : String(graph[i][$0])
26+
}.joined(separator: " "))
27+
}

0 commit comments

Comments
 (0)