-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1389.js
49 lines (36 loc) · 1.09 KB
/
1389.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
45
46
47
48
49
var readline = require("readline");
var r = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
let input = [];
r.on("line", function (line) {
input.push(line);
}).on("close", function () {
main(input);
process.exit();
});
function main(input) {
const [N,M] = input.shift().split(" ").map(Number)
const graph = [...Array(N)].map(() => [...Array(N)].fill(Infinity))
input.forEach(inp => {
const [x, y] = inp.split(' ').map(Number)
graph[x - 1][y - 1] = 1;
graph[y - 1][x - 1] = 1;
});
for(let k = 0; k < N ; k++){
for(let i = 0; i < N ; i++){
for(let j = 0; j < N ; j++){
if(i == j){
graph[i][j] = 0;
continue
}
if(graph[i][j] > graph[i][k] + graph[k][j]){
graph[i][j] = graph[i][k] + graph[k][j]
}
}
}
}
let bacun = graph.map((val) => val.reduce((pre,curr) => pre + curr, 0));
console.log(bacun.indexOf(Math.min.apply(null, bacun)) + 1);
}