Skip to content

Commit f59dde8

Browse files
committed
諛곕�� / �ы�
1 parent 4cc360d commit f59dde8

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

JustDevRae/Graph/delivery.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
function solution(N, road, K) {
2+
const graph = Array.from({ length: N + 1 }, () => []);
3+
for (const [a, b, c] of road) {
4+
graph[a].push({ node: b, cost: c });
5+
graph[b].push({ node: a, cost: c });
6+
}
7+
8+
const distance = new Array(N + 1).fill(Infinity);
9+
distance[1] = 0;
10+
11+
const visited = new Array(N + 1).fill(false);
12+
13+
for (let i = 1; i <= N; i++) {
14+
let current = -1;
15+
16+
for (let j = 1; j <= N; j++) {
17+
if (!visited[j] && (current === -1 || distance[j] < distance[current])) {
18+
current = j;
19+
}
20+
}
21+
22+
if (current === -1) break;
23+
24+
visited[current] = true;
25+
26+
for (const { node, cost } of graph[current]) {
27+
if (distance[node] > distance[current] + cost) {
28+
distance[node] = distance[current] + cost;
29+
}
30+
}
31+
}
32+
return distance.filter((time) => time <= K).length;
33+
}

0 commit comments

Comments
 (0)