Two algorithms JS Solution
\nvar networkDelayTime = function(times, N, K) {\n    // Converting data into adjacency list\n    let adjList = new Array(N).fill(0).map(() => []);\n    for (le
```\nvar networkDelayTime = function(times, N, K) {\n    // Converting data into adjacency list\n    let adjList = new Array(N).fill(0).map(() => []);\n    for (let edge of times) {\n        adjList[edge[0]-1].push([edge[1]-1,edge[2]]);\n    }\n    \n    let travelTime = new Array(N).fill(Infinity);\n    travelTime[K-1] = 0;\n    let heap = new PriorityQueue((a,b) => travelTime[a-1] < travelTime[b-1]);\n    heap.push(K-1);\n    \n    while (!heap.isEmpty()) {\n        let curV = heap.pop();\n        for (let pair of adjList[curV]) {\n            let nextV = pair[0], weight = pair[1];\n            let tempTime = travelTime[curV] + weight;\n            if (travelTime[nextV] > tempTime) {\n                travelTime[nextV] = tempTime;\n                heap.push(nextV);\n                // costs to vertices connected to this nextV might change\n                // if cost to this nextV changed\n                // but if cost to this nextV didn\'t change, there is no need to push\n                // this vertex to heap again\n            }\n        }\n    }\n    \n    // travelTime[v] represents the lowest-cost path from K to v vertex \n    // Find the highest cost to reach a vertex because at this cost\n    // K must have visited all other vertices already\n    let totalTime = Math.max(...travelTime);\n    return totalTime === Infinity ? -1 : totalTime;\n};\n\n// Dijkstra\'s algorithm\n// Time Complexity: O(N + ELog(E)) where E can be N^2 in the worst case\n// Space Complexity: O(N + E)\n\n// Priority Queue implementation\nclass PriorityQueue {\n  constructor(comparator = (a, b) => a > b) {\n    this._heap = [];\n    this._comparator = comparator;\n  }\n\n  size() {\n    return this._heap.length;\n  }\n\n  peek() {\n    return this._heap[0];\n  }\n\n  isEmpty() {\n    return this._heap.length === 0;\n  }\n\n  _parent(idx) {\n    return Math.floor((idx - 1) / 2);\n  }\n\n  _leftChild(idx) {\n    return idx * 2 + 1;\n  }\n\n  _rightChild(idx) {\n    return idx * 2 + 2;\n  }\n\n  _swap(i, j) {\n    [this._heap[i], this._heap[j]] = [this._heap[j], this._heap[i]];\n  }\n\n  _compare(i, j) {\n    return this._comparator(this._heap[i], this._heap[j]);\n  }\n\n  push(value) {\n    this._heap.push(value);\n    this._siftUp();\n\n    return this.size();\n  }\n\n  _siftUp() {\n    let nodeIdx = this.size() - 1;\n\n    while (0 < nodeIdx && this._compare(nodeIdx, this._parent(nodeIdx))) {\n      this._swap(nodeIdx, this._parent(nodeIdx));\n      nodeIdx = this._parent(nodeIdx);\n    }\n  }\n\n  pop() {\n    if (this.size() > 1) {\n      this._swap(0, this.size() - 1);\n    }\n\n    const poppedValue = this._heap.pop();\n    this._siftDown();\n    return poppedValue;\n  }\n\n  _siftDown() {\n    let nodeIdx = 0;\n\n    while (\n      (this._leftChild(nodeIdx) < this.size() &&\n        this._compare(this._leftChild(nodeIdx), nodeIdx)) ||\n      (this._rightChild(nodeIdx) < this.size() &&\n        this._compare(this._rightChild(nodeIdx), nodeIdx))\n    ) {\n      const greaterChildIdx =\n        this._rightChild(nodeIdx) < this.size() &&\n        this._compare(this._rightChild(nodeIdx), this._leftChild(nodeIdx))\n          ? this._rightChild(nodeIdx)\n          : this._leftChild(nodeIdx);\n\n      this._swap(greaterChildIdx, nodeIdx);\n      nodeIdx = greaterChildIdx;\n    }\n  }\n}\n```\n\n```\nvar networkDelayTime = function(times, N, K) {\n  let distances = new Array(N).fill(Infinity);\n  distances[K-1] = 0;\n  \n  for (let i = 0; i < N-1; i++) {\n      let cost = 0;\n      for (let edge of times) {\n          let source = edge[0], target = edge[1], weight = edge[2];\n          let newDistance = distances[source-1] + weight;\n          if (newDistance < distances[target-1]) {\n              distances[target-1] = newDistance;\n              cost++;\n          }\n      }\n      if (cost === 0) break; // already found all shortest distances to every vertex\n  }\n  let res = Math.max(...distances);\n  return res === Infinity ? -1 : res;\n}\n// Bellman-Ford\'s algorithm\n// Time Complexity: O(N*E) where E can be N^2 in the worst case\n// Space Complexity: O(N)\n```
['JavaScript']