Skip to content

Commit 57e8a28

Browse files
committed
출력을 잘못했었네;;
1 parent bca5072 commit 57e8a28

1 file changed

Lines changed: 42 additions & 0 deletions

File tree

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
}

0 commit comments

Comments
 (0)