Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions src/C9 최단 경로/Q37 JS2 플로이드.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
function solution(n, m, cities) {
let graph = Array(n)
.fill()
.map(() => Array(n).fill(Infinity));
for (let i = 0; i < n; i++) {
graph[i][i] = 0;
}
cities.forEach(
([start, end, cost]) =>
graph[start - 1][end - 1] > cost && (graph[start - 1][end - 1] = cost)
);
Comment on lines +8 to +11
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
cities.forEach(
([start, end, cost]) =>
graph[start - 1][end - 1] > cost && (graph[start - 1][end - 1] = cost)
);
cities
.filter((start, end, cost]) => graph[start - 1][end - 1] > cost)
.forEach( (start, end, cost]) => (graph[start - 1][end - 1] = cost));
for(const [start, end, cost] of cities) {
  if([start - 1][end - 1] > cost)
    graph[start - 1][end - 1] = cost;
}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

언제나 고칠점을 발견해주셔서 감사합니다!!

for (let i = 0; i < n; i++) {
for (let a = 0; a < n; a++) {
for (let b = 0; b < n; b++) {
graph[a][b] = Math.min(graph[a][b], graph[a][i] + graph[i][b]);
}
}
}
for (let a = 0; a < n; a++) {
for (let b = 0; b < n; b++) {
graph[a][b] === Infinity && (graph[a][b] = 0);
}
}

return graph;
}
const n = 5;
const m = 14;
const cities = [
[1, 2, 2],
[1, 3, 3],
[1, 4, 1],
[1, 5, 10],
[2, 4, 2],
[3, 4, 1],
[3, 5, 1],
[4, 5, 3],
[3, 5, 10],
[3, 1, 8],
[1, 4, 2],
[5, 1, 7],
[3, 4, 2],
[5, 2, 4],
];

console.log(solution(n, m, cities));