public static ListNode MergeKLists(ListNode[] lists) { if (lists.Length == 0) { return(null); } ListNode head = null; ListNode cur = null; // min queue var priorityQueue = new PriorityQueuList <QueueNode>((x, y) => x.node.val - y.node.val); for (int i = 0; i < lists.Length; i++) { if (lists[i] != null) { priorityQueue.Enqueue(new QueueNode(lists[i], i)); } } while (!priorityQueue.Empty) { var minNode = priorityQueue.Dequeue(); var loc = minNode.location; var newNode = minNode.node; if (head == null) { cur = newNode; head = newNode; } else { cur.next = newNode; cur = cur.next; } lists[loc] = lists[loc].next; if (lists[loc] != null) { priorityQueue.Enqueue(new QueueNode(lists[loc], loc)); } } return(head); }
// Dijkstra public override void ShortestPath() { Init(); while (!queue.Empty) { var v = queue.Dequeue(); foreach (var w in graph.GetNeighbours(v)) { var edge = graph.GetEdge(v, w); int heuristicDistance = heuristic.Distance(w, target); var newDistance = Distance[(int)v.ID] + edge.Value + heuristicDistance; if (newDistance < Distance[(int)w.ID]) { Distance[(int)w.ID] = newDistance; PreviousVertex[(int)w.ID] = v; } } } }