File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments