Skip to content

Commit 0b54ae2

Browse files
committed
106차 1번 문제풀이
1 parent 78ba978 commit 0b54ae2

1 file changed

Lines changed: 56 additions & 0 deletions

File tree

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
const input = require("fs")
2+
.readFileSync(
3+
process.platform === "linux"
4+
? "/dev/stdin"
5+
: require("path").join(__dirname, "input.txt"),
6+
"utf8"
7+
)
8+
.toString()
9+
.trim()
10+
.split("\n");
11+
12+
function solution(input) {
13+
const n = Number(input[0]);
14+
const m = Number(input[1]);
15+
16+
const map = Array.from({ length: n + 1 }, () => Array(n + 1).fill(Infinity));
17+
18+
for (let i = 1; i <= n; i++) {
19+
map[i][i] = 0;
20+
}
21+
22+
for (let i = 2; i < 2 + m; i++) {
23+
const [a, b, c] = input[i].split(" ").map(Number);
24+
if (map[a][b] > c) {
25+
map[a][b] = c;
26+
}
27+
}
28+
29+
for (let k = 1; k <= n; k++) {
30+
for (let i = 1; i <= n; i++) {
31+
for (let j = 1; j <= n; j++) {
32+
if (map[i][j] > map[i][k] + map[k][j]) {
33+
map[i][j] = map[i][k] + map[k][j];
34+
}
35+
}
36+
}
37+
}
38+
39+
const answer = [];
40+
41+
for (let i = 1; i <= n; i++) {
42+
let line = [];
43+
for (let j = 1; j <= n; j++) {
44+
if (map[i][j] === Infinity) {
45+
line.push(0);
46+
} else {
47+
line.push(map[i][j]);
48+
}
49+
}
50+
answer.push(line.join(" "));
51+
}
52+
53+
return answer.join("\n");
54+
}
55+
56+
console.log(solution(input));

0 commit comments

Comments
 (0)