Minimum priority queue in C++

Revision en4, by Evang, 2020-10-24 06:25:30

Hello Codeforces!

I've seen people using negative edges when creating a priority queue because writing a minimum is simply too tedious. And, I gotta say, I agree with them! Look at this code when you have to declare a min heap for a pair of ints:

priority_queue<pair<int, int>, vector<pair<int, int>>, greater<>> q;

That simply is too much to write! However, there is a simple solution. Just include this somewhere near the top of your code:

template<typename T>
using min_heap = priority_queue<T, vector<T>, greater<T>>;

Now you can declare a min heap of pairs of ints by writing:

min_heap<pair<int, int>> q;

To me, that is more clear and concise, and less to type! Are there any other reasons that some people might create a min heap by negating edge weights instead of writing that block of code at the top?

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en4 English Evang 2020-10-24 06:25:30 56
en3 English Evang 2020-10-24 06:23:55 28 Tiny change: 'pe!\nNow, why is it that some' -> 'pe!\nNow, are there any other reasons that some'
en2 English Evang 2020-10-24 06:22:52 0 (published)
en1 English Evang 2020-10-24 06:21:18 820 Initial revision (saved to drafts)