-
Notifications
You must be signed in to change notification settings - Fork 0
/
1238_2.js
44 lines (36 loc) · 898 Bytes
/
1238_2.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
const fs = require('fs');
function solve() {
const input = fs.readFileSync('/dev/stdin').toString().split('\n');
let inputIdx = 0;
const inf = 1_000_000_000;
const [N, M, X] = input[inputIdx++].split(' ').map((v) => +v);
// arr 초기화
const arr = [];
arr.length = N + 1;
for (let i = 1; i <= N; i++) {
arr[i] = [];
arr[i].length = N + 1;
for (let j = 1; j <= N; j++) {
arr[i][j] = inf;
}
arr[i][i] = 0;
}
for (let i = 0; i < M; i++) {
const [from, to, dist] = input[inputIdx++].split(' ').map((v) => +v);
arr[from][to] = dist;
}
let tmp;
for (let k = 1; k <= N; k++) {
for (let i = 1; i <= N; i++) {
for (let j = 1; j <= N; j++) {
if ((tmp = arr[i][k] + arr[k][j]) < arr[i][j]) arr[i][j] = tmp;
}
}
}
let max = -1;
for (let i = 1; i <= N; i++) {
if (max < (tmp = arr[i][X] + arr[X][i])) max = tmp;
}
return max;
}
console.log(solve());