1
- const initializeGraph = ( n ) => {
2
- let G = [ ]
3
- for ( let i = 0 ; i < n ; i ++ ) {
4
- G . push ( [ ] )
5
- }
6
- return G
7
- }
8
- const addEdgeToG = ( G , Edges ) => {
9
- for ( const [ u , v ] of Edges ) {
10
- G [ u ] . push ( v )
11
- G [ v ] . push ( u )
12
- }
13
- }
14
1
/**
15
2
* @param {number } n
16
3
* @param {number[][] } edges
17
4
* @param {number } time
18
5
* @param {number } change
19
6
* @return {number }
20
7
*/
21
- const secondMinimum = ( n , edges , time , change ) => {
8
+ var secondMinimum = function ( n , edges , time , change ) {
22
9
let adj = initializeGraph ( n + 1 )
23
10
addEdgeToG ( adj , edges )
24
11
let cost = initializeGraph ( n + 1 )
25
- let pq = new MinPriorityQueue ( { priority : ( x ) => x [ 0 ] } )
26
- pq . enqueue ( [ 0 , 1 ] )
12
+ let pq = new PQ ( ( a , b ) => a [ 0 ] < b [ 0 ] )
13
+ pq . push ( [ 0 , 1 ] )
27
14
let green = 2 * change
28
15
while ( pq . size ( ) ) {
29
- let cur = pq . dequeue ( ) . element
16
+ let cur = pq . pop ( )
30
17
let [ t , node ] = cur
31
18
if ( cost [ node ] . length == 2 ) continue
32
19
let nextT =
@@ -45,9 +32,89 @@ const secondMinimum = (n, edges, time, change) => {
45
32
continue
46
33
}
47
34
}
48
- for ( const next_node of adj [ node ] ) pq . enqueue ( [ nextT + time , next_node ] )
35
+ for ( const next_node of adj [ node ] ) pq . push ( [ nextT + time , next_node ] )
49
36
}
50
37
return cost [ n ] [ 1 ]
38
+ } ;
39
+ function initializeGraph ( n ) {
40
+ let G = [ ]
41
+ for ( let i = 0 ; i < n ; i ++ ) {
42
+ G . push ( [ ] )
43
+ }
44
+ return G
45
+ }
46
+ function addEdgeToG ( G , Edges ) {
47
+ for ( const [ u , v ] of Edges ) {
48
+ G [ u ] . push ( v )
49
+ G [ v ] . push ( u )
50
+ }
51
+ }
52
+ class PQ {
53
+ constructor ( comparator = ( a , b ) => a > b ) {
54
+ this . heap = [ ]
55
+ this . top = 0
56
+ this . comparator = comparator
57
+ }
58
+ size ( ) {
59
+ return this . heap . length
60
+ }
61
+ isEmpty ( ) {
62
+ return this . size ( ) === 0
63
+ }
64
+ peek ( ) {
65
+ return this . heap [ this . top ]
66
+ }
67
+ push ( ...values ) {
68
+ values . forEach ( ( value ) => {
69
+ this . heap . push ( value )
70
+ this . siftUp ( )
71
+ } )
72
+ return this . size ( )
73
+ }
74
+ pop ( ) {
75
+ const poppedValue = this . peek ( )
76
+ const bottom = this . size ( ) - 1
77
+ if ( bottom > this . top ) {
78
+ this . swap ( this . top , bottom )
79
+ }
80
+ this . heap . pop ( )
81
+ this . siftDown ( )
82
+ return poppedValue
83
+ }
84
+ replace ( value ) {
85
+ const replacedValue = this . peek ( )
86
+ this . heap [ this . top ] = value
87
+ this . siftDown ( )
88
+ return replacedValue
89
+ }
90
+
91
+ parent = ( i ) => ( ( i + 1 ) >>> 1 ) - 1
92
+ left = ( i ) => ( i << 1 ) + 1
93
+ right = ( i ) => ( i + 1 ) << 1
94
+ greater = ( i , j ) => this . comparator ( this . heap [ i ] , this . heap [ j ] )
95
+ swap = ( i , j ) => ( [ this . heap [ i ] , this . heap [ j ] ] = [ this . heap [ j ] , this . heap [ i ] ] )
96
+ siftUp = ( ) => {
97
+ let node = this . size ( ) - 1
98
+ while ( node > this . top && this . greater ( node , this . parent ( node ) ) ) {
99
+ this . swap ( node , this . parent ( node ) )
100
+ node = this . parent ( node )
101
+ }
102
+ }
103
+ siftDown = ( ) => {
104
+ let node = this . top
105
+ while (
106
+ ( this . left ( node ) < this . size ( ) && this . greater ( this . left ( node ) , node ) ) ||
107
+ ( this . right ( node ) < this . size ( ) && this . greater ( this . right ( node ) , node ) )
108
+ ) {
109
+ let maxChild =
110
+ this . right ( node ) < this . size ( ) &&
111
+ this . greater ( this . right ( node ) , this . left ( node ) )
112
+ ? this . right ( node )
113
+ : this . left ( node )
114
+ this . swap ( node , maxChild )
115
+ node = maxChild
116
+ }
117
+ }
51
118
}
52
119
53
120
// another
0 commit comments