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+ let n = Int ( readLine ( ) !) !
2+ let m = Int ( readLine ( ) !) !
3+ var graph = Array ( repeating: Array ( repeating: Int . max, count: n + 1 ) , count: n + 1 )
4+
5+ for _ in 0 ..< m {
6+ let component = readLine ( ) !. split ( separator: " " ) . compactMap { Int ( $0) }
7+ let start = component [ 0 ]
8+ let end = component [ 1 ]
9+ let cost = component [ 2 ]
10+
11+ // 중복 있음;; 최솟값만 넣기
12+ if graph [ start] [ end] > cost { graph [ start] [ end] = cost }
13+ }
14+
15+ // for i in 1..<graph.count {
16+ // let row = graph[i].map { $0 == Int.max ? "-": String($0) }.joined(separator: " ")
17+ // print(row)
18+ // }
19+
20+ // 경유
21+ for k in 1 ... n {
22+ // 시작
23+ for i in 1 ... n {
24+ // 도착
25+ for j in 1 ... n {
26+ if i == j { graph [ i] [ j] = 0 }
27+ if graph [ i] [ k] != Int . max && graph [ k] [ j] != Int . max {
28+ let cost = graph [ i] [ k] + graph[ k] [ j]
29+ if graph [ i] [ j] > cost {
30+ graph [ i] [ j] = cost
31+ }
32+ }
33+ }
34+ }
35+ }
36+
37+ // 만약, i에서 j로 갈 수 없는 경우에는 그 자리에 0을 출력한다.
38+ for i in 1 ..< graph. count {
39+ graph [ i] . removeFirst ( )
40+ let row = graph [ i] . map { $0 == Int . max ? " 0 " : String ( $0) } . joined ( separator: " " )
41+ print ( row)
42+ }
You can’t perform that action at this time.
0 commit comments