Skip to content

Commit 7d247fe

Browse files
authored
Create 882-reachable-nodes-in-subdivided-graph.js
1 parent b7a0c4e commit 7d247fe

File tree

1 file changed

+105
-0
lines changed

1 file changed

+105
-0
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/**
2+
* @param {number[][]} edges
3+
* @param {number} M
4+
* @param {number} N
5+
* @return {number}
6+
*/
7+
const reachableNodes = function (edges, M, N) {
8+
const graph = Array.from({ length: N }, () => Array(N).fill(-1))
9+
for (let edge of edges) {
10+
graph[edge[0]][edge[1]] = edge[2]
11+
graph[edge[1]][edge[0]] = edge[2]
12+
}
13+
let result = 0
14+
const pq = new PriorityQueue((a, b) => a[1] > b[1])
15+
const visited = new Array(N).fill(false)
16+
pq.push([0, M])
17+
while (!pq.isEmpty()) {
18+
const cur = pq.pop()
19+
const start = cur[0]
20+
const move = cur[1]
21+
if (visited[start]) {
22+
continue
23+
}
24+
visited[start] = true
25+
result++
26+
for (let i = 0; i < N; i++) {
27+
if (graph[start][i] > -1) {
28+
if (move > graph[start][i] && !visited[i]) {
29+
pq.push([i, move - graph[start][i] - 1])
30+
}
31+
graph[i][start] -= Math.min(move, graph[start][i])
32+
result += Math.min(move, graph[start][i])
33+
}
34+
}
35+
}
36+
return result
37+
}
38+
39+
class PriorityQueue {
40+
constructor(comparator = (a, b) => a > b) {
41+
this.heap = []
42+
this.top = 0
43+
this.comparator = comparator
44+
}
45+
size() {
46+
return this.heap.length
47+
}
48+
isEmpty() {
49+
return this.size() === 0
50+
}
51+
peek() {
52+
return this.heap[this.top]
53+
}
54+
push(...values) {
55+
values.forEach((value) => {
56+
this.heap.push(value)
57+
this.siftUp()
58+
})
59+
return this.size()
60+
}
61+
pop() {
62+
const poppedValue = this.peek()
63+
const bottom = this.size() - 1
64+
if (bottom > this.top) {
65+
this.swap(this.top, bottom)
66+
}
67+
this.heap.pop()
68+
this.siftDown()
69+
return poppedValue
70+
}
71+
replace(value) {
72+
const replacedValue = this.peek()
73+
this.heap[this.top] = value
74+
this.siftDown()
75+
return replacedValue
76+
}
77+
78+
parent = (i) => ((i + 1) >>> 1) - 1
79+
left = (i) => (i << 1) + 1
80+
right = (i) => (i + 1) << 1
81+
greater = (i, j) => this.comparator(this.heap[i], this.heap[j])
82+
swap = (i, j) => ([this.heap[i], this.heap[j]] = [this.heap[j], this.heap[i]])
83+
siftUp = () => {
84+
let node = this.size() - 1
85+
while (node > this.top && this.greater(node, this.parent(node))) {
86+
this.swap(node, this.parent(node))
87+
node = this.parent(node)
88+
}
89+
}
90+
siftDown = () => {
91+
let node = this.top
92+
while (
93+
(this.left(node) < this.size() && this.greater(this.left(node), node)) ||
94+
(this.right(node) < this.size() && this.greater(this.right(node), node))
95+
) {
96+
let maxChild =
97+
this.right(node) < this.size() &&
98+
this.greater(this.right(node), this.left(node))
99+
? this.right(node)
100+
: this.left(node)
101+
this.swap(node, maxChild)
102+
node = maxChild
103+
}
104+
}
105+
}

0 commit comments

Comments
 (0)