public int next() { while (!Utils.is_heap_empty(heap_queue)) { QueueElement top = Utils.heap_delete_min(heap_queue); Node rnode = top.node; if (rnode.x == 0) { for (QueueElement n = top; n != null; n = n.next) { result_[n.node.x] = n.node.y; } cost_ = top.gx; return(0); } foreach (Path p in rnode.lpathList) { QueueElement n = Utils.allc_from_heap(heap_queue); int x_num = (rnode.x) - 1; n.node = p.lnode; n.gx = -p.lnode.cost - p.cost + top.gx; n.fx = -p.lnode.bestCost - p.cost + top.gx; // | h(x) | | g(x) | n.next = top; if (Utils.heap_insert(n, heap_queue) < 0) { return(Utils.ERROR_INSERT_HEAP_FAILED); } } } return(0); }
public static int heap_insert(QueueElement qe, Heap H) { if (H.size >= H.capacity) { return Utils.ERROR_HEAP_SIZE_TOO_BIG; } var i = ++H.size; while (i != 1 && H.elem_ptr_list[i / 2].fx > qe.fx) { H.elem_ptr_list[i] = H.elem_ptr_list[i / 2]; //此时i还没有进行i/2操作 i /= 2; } H.elem_ptr_list[i] = qe; return 0; }
public static int heap_insert(QueueElement qe, Heap H) { if (H.size >= H.capacity) { return(Utils.ERROR_HEAP_SIZE_TOO_BIG); } int i = ++H.size; while (i != 1 && H.elem_ptr_list[i / 2].fx > qe.fx) { H.elem_ptr_list[i] = H.elem_ptr_list[i / 2]; //此时i还没有进行i/2操作 i /= 2; } H.elem_ptr_list[i] = qe; return(0); }
public int initNbest() { int k = (int)word_num - 1; for (int i = 0; i < ysize_; ++i) { QueueElement eos = Utils.allc_from_heap(heap_queue); eos.node = node_[k, i]; eos.fx = -node_[k, i].bestCost; eos.gx = -node_[k, i].cost; eos.next = null; if (Utils.heap_insert(eos, heap_queue) < 0) { return(Utils.ERROR_INSERT_HEAP_FAILED); } } return(Utils.ERROR_SUCCESS); }
public static QueueElement heap_delete_min(Heap H) { QueueElement min_elem = H.elem_ptr_list[1]; //堆是从第1号元素开始的 QueueElement last_elem = H.elem_ptr_list[H.size--]; int i = 1, ci = 2; while (ci <= H.size) { if (ci < H.size && H.elem_ptr_list[ci].fx > H.elem_ptr_list[ci + 1].fx) { ci++; } if (last_elem.fx <= H.elem_ptr_list[ci].fx) { break; } H.elem_ptr_list[i] = H.elem_ptr_list[ci]; i = ci; ci *= 2; } H.elem_ptr_list[i] = last_elem; return(min_elem); }