Heap-implemented priority queue Template

Source: Internet
Author: User

The following is a self-written priority queue template (by default, it is a large top heap, which can be changed by reloading a smaller number)

The usage instructions are as follows:

1) priority_queue (reference STL for usage)

The usage of priority_queue in the C-free library function help is described as follows:

The C ++ priority queue is similar to a queue, but the elements in the data structure are sorted according to certain assertions.

Empty () If the priority queue is empty, true is returned.
Pop () Delete the first element
Push () Add an element
Size () Returns the number of elements in the priority queue.
Top () Returns the element with the highest priority in the priority queue.

2) Basic heap operations.

The usage is as follows:

Set_size () Set heap size
Set_value () Set the value of the ID element in the heap.
Creat_heap () Create an initial heap
Heap_sort () Heap sorting, from small to large by default. You can sort by specified rules by reloading the smaller number.
Is_heap () Determine whether a heap is formed

The template is as follows:

# Define maxn 25500 typedef struct redtype {int left, right; int weight; bool friend operator <(const redtype & A, const redtype & B) {return. weight> B. weight ;}} edge; Class priority_queue {PRIVATE: redtype R [maxn]; int length; void heap_adjust (int s, int m) {redtype rc = R [s]; for (Int J = S <1; j <= m; j <= 1) {If (j <M & R [J] <R [J + 1]) J ++; If (! (RC <R [J]) break; R [s] = R [J]; S = J;} R [s] = RC;} public: priority_queue () {length = 0 ;}/ * ============ The following is the heap operation section, heap and so on ================= */void creat_heap () {for (INT I = length> 1; I> 0; I --) {heap_adjust (I, length) ;}} void set_size (INT length) {This-> length = length;} void set_value (INT ID, redtype value) {R [ID] = value;} void heap_sort () {for (INT I = length; I> 1; I --) {redtype TMP = R [1]; R [1] = R [I]; R [I] = TMP; heap_adjust (1, I-1) ;}} bool is_heap () {int Len = length> 1-1, J; if (LEN <1) {If (length = 1 | (! (R [1] <R [length]) &! (R [1] <R [length-1]) return true; return false;} For (INT I = 1; I <= Len; I ++) {J = I <1; if (R [J] <R [J + 1]) J ++; If (R [I] <R [J]) return false;} return true;}/* = although the following functions are not powerful in STL, their usage is similar (using heap-implemented priority_queue) = */void push (redtype RC) {++ length; R [length] = RC; int S = length> 1; for (Int J = length; j> 1; j >>= 1) {If (! (R [s] <R [J]) break; redtype TMP = R [s]; R [s] = R [J]; R [J] = TMP; s >>=1 ;}} void POP () {redtype TMP = R [1]; R [1] = R [length]; R [length] = TMP; heap_adjust (1, -- length);} redtype top () {return R [1];} int size () {return length;} bool empty () {If (length <= 0) return true; return false ;}};

The basic functions and complexity analysis are as follows:

Heap_adjust () is adjusted downward from the first node s that does not meet the heap requirements to maintain a heap.

-------- Log (N)

Push () adds a leaf node from the tail to the heap. Because the heap is satisfied, you only need to switch from bottom to top with the parent node until the heap has been satisfied and exited.

-------- Log (N)

Pop () Exchanges start and end, and then [start, end-1] makes a new adjustment to maintain [start, end-1] As a heap, the heap size length is-1.

-------- Log (N)

Creat_heap () starts from the first non-terminal node-> the root node continuously performs heap_adjust () to form an initial heap.

-------- N/2 * log (N)

Heap_sort () is equivalent to repeating n POP () operations, but the length does not change.

-------- N * log (N)

Is_heap () is used to determine whether a heap is formed. If the heap size is smaller than 4, it is determined by the if outside to avoid additional Judgment in the for loop.

-------- N/2.

Template example:

Http://acm.hdu.edu.cn/showproblem.php? PID = 3371 min Spanning Tree

3358493 21:34:41 accepted 3371 500 ms 484 k 3659 B C ++ slave_wc
# Include <stdio. h> # include <string. h> # define maxn 25500 # define maxn 25500 typedef struct redtype {int left, right; int weight; bool friend operator <(const redtype & A, const redtype & B) {return. weight> B. weight ;}} edge; Class priority_queue {PRIVATE: redtype R [maxn]; int length; void heap_adjust (int s, int m) {redtype rc = R [s]; for (Int J = S <1; j <= m; j <= 1) {If (j <M & R [J] <R [J + 1]) J ++; if (! (RC <R [J]) break; R [s] = R [J]; S = J;} R [s] = RC;} public: priority_queue () {length = 0 ;}/ * ============ The following is the heap operation section, heap and so on ================= */void creat_heap () {for (INT I = length> 1; I> 0; I --) {heap_adjust (I, length) ;}} void set_size (INT length) {This-> length = length;} void set_value (INT ID, redtype value) {R [ID] = value;} void heap_sort () {for (INT I = length; I> 1; I --) {redtype TMP = R [1]; R [1] = R [I]; R [I] = R [1]; heap_adjust (1, I-1) ;}} bool is_heap () {int Len = length> 1-1, J; if (LEN <1) {If (length = 1 | (! (R [1] <R [length]) &! (R [1] <R [length-1]) return true; return false;} For (INT I = 1; I <= Len; I ++) {J = I <1; if (R [J] <R [J + 1]) J ++; If (R [I] <R [J]) return false;} return true;}/* = although the following functions are not powerful in STL, their usage is similar (using heap-implemented priority_queue) = */void push (redtype RC) {++ length; R [length] = RC; int S = length> 1; for (Int J = length; j> 1; j >>= 1) {If (! (R [s] <R [J]) break; redtype TMP = R [s]; R [s] = R [J]; R [J] = TMP; s >>=1 ;}} void POP () {redtype TMP = R [1]; R [1] = R [length]; R [length] = TMP; heap_adjust (1, -- length);} redtype top () {return R [1];} int size () {return length;} bool empty () {If (length <= 0) return true; return false ;}}; priority_queue heap; int set [maxn]; bool hash [maxn]; int find (int x) {int r = x; while (R! = Set [R]) r = set [R]; int I = x, J; while (I! = R) {J = set [I]; set [I] = r; I = J;} return r;} bool merge (INT X, int y) {int FX = find (x); int FY = find (y); If (FX! = FY) {set [FX] = FY; return true;} return false;} void Init (int n) {for (INT I = 0; I <= N; I ++) {set [I] = I; hash [I] = false ;}} void kruscal (INT N, int num); void input () {int test, n, m, K; edge cur; scanf ("% d", & Test); While (test --) {scanf ("% d", & N, & M, & K); Init (n); heap. set_size (m); For (INT I = 1; I <= m; I ++) {scanf ("% d", & cur. left, & cur. right, & cur. weight); heap. set_value (I, cur);} heap. crea T_heap (); int t, a, B, num = 0, NN = 0, nk = 0; For (INT I = 1; I <= K; I ++) {scanf ("% d", & T, & A); hash [a] = true; For (Int J = 2; j <= T; j ++) {scanf ("% d", & B); merge (a, B); hash [B] = true ;}} for (INT I = 1; I <= N; I ++) if (hash [I]) nn ++; For (INT I = 1; I <= N; I ++) {If (hash [I] & set [I] = I) nk ++;} num = nn-nk; kruscal (n, num );}} void kruscal (int n, int num) {int sum = 0; while (! Heap. empty () & num <n-1) {edge cur = heap. top (); heap. pop (); If (merge (cur. left, cur. right) {sum + = cur. weight; num ++ ;}}if (num = n-1) {printf ("% d \ n", sum );} else {puts ("-1") ;}} int main () {input (); Return 0 ;}

Efficiency Analysis:

AboveCodeThe heap is built through creat_heap (). If the heap is pushed one by one, the complexity analysis above shows that the complexity of the heap process is doubled.

(N/2 * log (n) to N * log (n ))

After testing, the code above is changed to the method of pushing the heap one by one through push, and the efficiency is 700 + MS.

If you use the priority_queue that comes with the # include <queue> header file, the heap is also created through push, with an efficiency of more than 800 ms.

OriginalArticleIf it is reproduced, please note: transferred from ¥ forgot % windHttp://www.cnblogs.com/slave_wc}

Address:

Heap implemented priority queue template {http://www.cnblogs.com/slave_wc/archive/2010/12/23/1915293.html}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.