AcWing 854. Floyd求最短路(Javascript)
原题链接
简单
作者:
cp777
,
2021-03-02 17:09:54
,
所有人可见
,
阅读 249
let N = 210;
let M = 20010;
let INF = 0x3f3f3f3f;
let graph = [];
for (let i = 0; i < N; i++) graph[i] = new Int32Array(N).fill(INF);
for (let i = 0; i < N; i++) graph[i][i] = 0;
let floyd = () => {
for (let k = 1; k <= n; k++) {
for (let i = 1; i <= n; i++) {
for (let j = 1; j <= n; j++) {
graph[i][j] = Math.min(graph[i][j], graph[i][k] + graph[k][j]);
}
}
}
}
let n = 0,
m = 0,
k = 0;
let buf = '';
process.stdin.on('readable', function () {
let chunk = process.stdin.read();
if (chunk) buf += chunk.toString();
});
let getInputNums = line => line.split(' ').filter(s => s !== '').map(x => parseInt(x));
let getInputStr = line => line.split(' ').filter(s => s !== '');
process.stdin.on('end', function () {
buf.split('\n').forEach(function (line, lineIdx) {
if (lineIdx === 0) {
n = getInputNums(line)[0];
m = getInputNums(line)[1];
k = getInputNums(line)[2];
} else if (lineIdx <= m) {
let arr = getInputNums(line);
let a = arr[0];
let b = arr[1];
let c = arr[2];
graph[a][b] = Math.min(graph[a][b], c);
if (lineIdx === m) {
floyd();
}
} else if (lineIdx <= m + k) {
let arr = getInputNums(line);
let a = arr[0];
let b = arr[1];
if (graph[a][b] > parseInt(INF / 2)) console.log('impossible');
else console.log(graph[a][b]);
}
});
});