Swift solution with comments, very concise
It uses a standard BFS and doesn\'t appear to need a priority queue. Please enlighten me if in any case a priority queue will help reduce the time. \nI love Swi
It uses a standard BFS and doesn\'t appear to need a priority queue. Please enlighten me if in any case a priority queue will help reduce the time. \nI love Swift language dealing with hashmaps in such a concise manner.\n```\nclass Solution {\n\tfunc networkDelayTime(_ times: [[Int]], _ N: Int, _ K: Int) -> Int {\n\t\t\n\t\tvar graph: [Int: [Int: Int]] = [:]\n\t\t\n\t\t/* Build graph: [source: [destination: weight]] */\n\t\tfor t in times {\n\t\t\tgraph[t[0], default: [:]][t[1]] = t[2]\n\t\t}\n\t\t\n\t\t/* Distance map: [node: distance from K to here] */\n\t\tvar distMap: [Int: Int] = [:]\n\t\tdistMap[K] = 0\n\t\tvar q: [(node: Int, dist: Int)] = []\n\t\t\n\t\tq.append((node: K, dist: 0))\n\t\t\n\t\twhile !q.isEmpty {\n\t\t\tlet curr = q.removeLast()\n\t\t\t\n\t\t\t/* Get all neighbors of current node from graph */\n\t\t\tguard let nexts = graph[curr.node] else { continue }\n\t\t\t\n\t\t\tfor (dest, weight) in nexts {\n\t\t\t\tif let distToNext = distMap[dest], curr.dist + weight >= distToNext {\n\t\t\t\t\t/* If there already exists a shorter distance to this neighbor,\n\t\t\t\t\t we do not update distance to this neighbor */\n\t\t\t\t\tcontinue\n\t\t\t\t}\n\t\t\t\t/* Update the distance to this neighbor\n\t\t\t\t/ Add this neighbor in the queue */\n\t\t\t\tdistMap[dest] = curr.dist + weight\n\t\t\t\tq.append((node: dest, dist: curr.dist + weight))\n\t\t\t}\n\t\t}\n\t\treturn distMap.count == N ? distMap.values.max() ?? -1 : -1\n\t}\n}\n\n```
[]