'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var queue = require('@algorithm.ts/queue'); const BIGINT_ZERO = BigInt(0); BigInt(1); class Dijkstra { ZERO; INF; dist; bestFrom; done; Q; constructor(props) { this.ZERO = props.ZERO; this.INF = props.INF; this.bestFrom = []; this.dist = []; this.done = []; this.Q = new queue.PriorityQueue({ compare: (x, y) => x.cost - y.cost }); } dijkstra(graph, options = {}) { const { N, source, edges, G } = graph; const { ZERO, Q, bestFrom, dist, done } = this; const { INF = this.INF } = options; dist.length = N; done.length = N; dist.fill(INF, 0, N); done.fill(false, 0, N); Q.init(); Q.enqueue({ pos: source, cost: ZERO }); dist[source] = ZERO; let edge; let to; let candidate; while (Q.size > 0) { const o = Q.dequeue().pos; if (done[o]) continue; done[o] = true; for (let i = 0, g = G[o], _size = g.length; i < _size; ++i) { edge = edges[g[i]]; to = edge.to; candidate = (dist[o] + edge.cost); if (dist[to] > candidate) { dist[to] = candidate; bestFrom[to] = o; Q.enqueue({ pos: to, cost: candidate }); } } } return { INF, source, bestFrom, dist }; } } let _dijkstra = null; const dijkstra = (graph, options = {}) => { if (_dijkstra === null) { _dijkstra = new Dijkstra({ ZERO: 0, INF: Math.floor(Number.MAX_SAFE_INTEGER / 2) - 1, }); } return _dijkstra.dijkstra(graph, options); }; let _dijkstraBigint = null; const dijkstraBigint = (graph, options = {}) => { if (_dijkstraBigint === null) { _dijkstraBigint = new Dijkstra({ ZERO: BIGINT_ZERO, INF: BigInt(Number.MAX_SAFE_INTEGER), }); } return _dijkstraBigint.dijkstra(graph, options); }; exports.Dijkstra = Dijkstra; exports.default = dijkstra; exports.dijkstra = dijkstra; exports.dijkstraBigint = dijkstraBigint;