Skip to content

Commit 6094fd7

Browse files
committed
Daily Solutions With JS
1 parent e99375e commit 6094fd7

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
const initializeGraph = (n) => { let G = []; for (let i = 0; i < n; i++) { G.push([]); } return G; };
2+
const addEdgeToG = (G, Edges) => { for (const [u, v] of Edges) { G[u].push(v); G[v].push(u); } };
3+
4+
const secondMinimum = (n, edges, time, change) => {
5+
let adj = initializeGraph(n + 1);
6+
addEdgeToG(adj, edges);
7+
let cost = initializeGraph(n + 1);
8+
let pq = new MinPriorityQueue({ priority: x => x[0] });
9+
pq.enqueue([0, 1]);
10+
let green = 2 * change;
11+
while (pq.size()) {
12+
let cur = pq.dequeue().element;
13+
let [t, node] = cur;
14+
if (cost[node].length == 2) continue;
15+
let nextT = t % green < change ? t : ((t + green - 1) / green >> 0) * green;
16+
let cn = cost[node].length;
17+
if (node == n) {
18+
if (cn == 0 || cost[node][cn - 1] != t) {
19+
cost[node].push(t);
20+
} else {
21+
continue;
22+
}
23+
} else {
24+
if (cn == 0 || cost[node][cn - 1] != nextT) {
25+
cost[node].push(nextT);
26+
} else {
27+
continue;
28+
}
29+
}
30+
for (const next_node of adj[node]) pq.enqueue([nextT + time, next_node]);
31+
}
32+
return cost[n][1];
33+
};

0 commit comments

Comments
 (0)