Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A question about priority queues #1

Open
seonwee opened this issue Apr 13, 2024 · 1 comment
Open

A question about priority queues #1

seonwee opened this issue Apr 13, 2024 · 1 comment

Comments

@seonwee
Copy link

seonwee commented Apr 13, 2024

Hello, I saw in your code that you treat the position of index 1 as the top of the heap element, and in the function named solve, you have a comment that says heap[0] position is not used, so before you put the first element in the priority queue, you should put an occupancy number into the heap, such as 0, Otherwise, when the first element is put into the priority queue, the index of the element is 0 instead of 1. Here is the code that I think needs to be changed:

bool heap_empty() {
    return heap.size() == 1;//there is a placeholder element when the heap is empty.
}

bool solve() {
    //...
    heap.push_back(0);//add a placeholder element
    heap.reserve(N + 1); // heap[0] is not used
    heap_index.resize(N + 1);
    for (uint v = 1; v <= N; ++v)
        heap_push(v);
   //...
@seonwee seonwee closed this as completed May 5, 2024
@seonwee seonwee closed this as not planned Won't fix, can't repro, duplicate, stale May 5, 2024
@seonwee seonwee closed this as completed May 5, 2024
@nyuichi nyuichi reopened this May 5, 2024
@nyuichi
Copy link
Owner

nyuichi commented May 5, 2024

Aw sorry I didn’t notice. What you pointed out seems correct. I am going to make a patch when I have time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants