diff --git "a/src/C9 \354\265\234\353\213\250 \352\262\275\353\241\234/Q37 JS2 \355\224\214\353\241\234\354\235\264\353\223\234.js" "b/src/C9 \354\265\234\353\213\250 \352\262\275\353\241\234/Q37 JS2 \355\224\214\353\241\234\354\235\264\353\223\234.js" new file mode 100644 index 0000000..9dd344a --- /dev/null +++ "b/src/C9 \354\265\234\353\213\250 \352\262\275\353\241\234/Q37 JS2 \355\224\214\353\241\234\354\235\264\353\223\234.js" @@ -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) + ); + 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));