-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path11657.js
More file actions
35 lines (29 loc) · 1011 Bytes
/
11657.js
File metadata and controls
35 lines (29 loc) · 1011 Bytes
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
/*
음수인 그래프에서 최단경로 찾기
벨만-포드
총 N개의 정점이 있을 때 N-1번이면 최단경로를 찾을 수 있음
N번째에 최단경로 테이블에 변동이 생기면 음수 사이클이 있다는 뜻
*/
const INPUT_FILE = process.platform === 'linux' ? '/dev/stdin' : './input';
const [[cityCount], ...buses] = require('fs')
.readFileSync(INPUT_FILE)
.toString()
.trim()
.split('\n')
.map((line) => line.split(' ').map(Number));
const costs = Array.from({ length: cityCount + 1 }, () => Infinity);
costs[1] = 0;
for (let moveCount = 0; moveCount < cityCount - 1; moveCount += 1) {
buses.forEach(([start, end, time]) => {
costs[end] = Math.min(costs[start] + time, costs[end]);
});
}
const hasNegativeCycle = buses.some(([start, end, time]) => {
return costs[start] + time < costs[end];
});
const sol = costs.map((cost) => (cost === Infinity ? -1 : cost));
if (hasNegativeCycle) {
console.log(-1);
} else {
console.log(sol.slice(2).join('\n'));
}