JavaScript Clean, Easy Bellman-Ford 97% | 91%
\n\n\n\nfunction networkDelayTime(times, N, K) {\n  //Initialize array to track times to each node from start node.\n  const time = Array(N).fill(Infinity);\n  
![image](https://assets.leetcode.com/users/images/dcf6d492-fd87-44f2-817f-851e88cc3d93_1631218602.7475052.png)\n\n\n```\nfunction networkDelayTime(times, N, K) {\n  //Initialize array to track times to each node from start node.\n  const time = Array(N).fill(Infinity);\n  //Initialize Start node to be 0 since signal begins here.\n  //The -1 here and below is so node value === index value\n  //in time array\n  time[K-1] = 0;\n  let changed = true;\n  //Iterate through signal times lowering time-to-node each iteration\n  //until no changes are made to time array. Best possible\n  //times to node at index from start node will then be stored \n  //in time array.\n  while (changed) {\n    changed = false;\n    for (let [node1, node2, timeToNode] of times) {\n\t  node1--, node2--;\n\t  //If time to first node === Infinity, we haven\'t figured out\n\t  //How to reach this node yet.  Continue until we have.\n      if (time[node1] === Infinity) continue;\n      if (time[node2] > time[node1] + timeToNode) {\n          time[node2] = time[node1] + timeToNode;\n          changed = true;\n      }\n    }\n  }\n  \n  //Return maximum time in time array since this is how long\n  //it will take for signal to reach last node.\n  let res = Math.max(...time)\n  //If max time is infinity, at least one node cannot be reached, return -1.\n  //Else return max value from time array.\n  return res === Infinity ? -1 : res;\n};\n```
['JavaScript']