-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp_배달.js
More file actions
43 lines (40 loc) · 976 Bytes
/
p_배달.js
File metadata and controls
43 lines (40 loc) · 976 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
36
37
38
39
40
41
42
43
function solution(N, road, K) {
const dist = new Array(N + 1).fill(Infinity);
let stack = [[1, 0]];
const adj = Array.from(Array(N + 1), () => []);
for (let i = 0; i < road.length; i++) {
let road_item = road[i];
adj[road_item[0]].push([road_item[1], road_item[2]]);
adj[road_item[1]].push([road_item[0], road_item[2]]);
}
dist[1] = 0; // 1번 마을은 무조건 갈 수 있도록 처리
while (stack.length) {
let tmp = stack.pop();
let node = tmp[0];
let dis = tmp[1];
adj[node].forEach((el) => {
let nextNode = el[0];
let nextDis = el[1];
if (dis + nextDis < dist[nextNode]) {
dist[nextNode] = dis + nextDis;
stack.push([nextNode, dis + nextDis]);
}
});
}
return dist.filter((item) => item <= K).length;
}
const N = 5;
console.log(
solution(
N,
[
[1, 2, 1],
[2, 3, 3],
[5, 2, 2],
[1, 4, 2],
[5, 3, 1],
[5, 4, 2],
],
3
)
);