ASC2 C Hyperhuffman Priority queue

Source: Internet
Author: User

Test instructions: give you a sequence that allows you to make a sequence of the WPL of the Huffman Tree

Problem solving ideas: Priority queue directly to engage. Because the numbers are too large, a non-recursive solution is used.

Problem Solving Code:

`1 #pragmaComment (linker, "/stack:1024000000,1024000000")2 //File name:c.cpp3 //Author:darkdream4 //Created time:2015 April 14 Tuesday 18:35 42 seconds5 6#include <vector>7#include <list>8#include <map>9#include <Set>Ten#include <deque> One#include <stack> A#include <bitset> -#include <algorithm> -#include <functional> the#include <numeric> -#include <utility> -#include <sstream> -#include <iostream> +#include <iomanip> -#include <cstdio> +#include <cmath> A#include <cstdlib> at#include <cstring> -#include <ctime> -#include <queue> - #defineLL Long Long - #defineMAXN 500005 - using namespacestd; in structnode{ - LL v; to    intnum; + node () {} -Node (LL _v,int_num) { thev =_v; *num =_num; \$    }Panax Notoginseng    BOOL operator< (ConstNode &b)Const { -      returnV >B.V; the    }  + }; APriority_queue<node>Qu; the  + intN; - intch[maxn*4][2]; \$ intque[maxn*4]; \$ intdeep[maxn*4]; - LL A[MAXN]; - inttot; the intMain () { -Freopen ("huffman.in","R", stdin);WuyiFreopen ("Huffman.out","W", stdout); thescanf"%d",&n); -      for(inti =1; I <= N;i + +){ Wuscanf"%i64d",&A[i]);  - Qu.push (Node (a[i],i)); About     } \$tot =N; -      while(Qu.size ()! =1){ - node TA,TB; -TA =qu.top (); A Qu.pop (); +TB =qu.top (); the Qu.pop (); -         //printf ("%i64d%d%i64d%d\n", ta.v,ta.num,tb.v,tb.num); \$Tot + + ;  thech[tot][0] =Ta.num; thech[tot][1] =Tb.num; theQu.push (Node (ta.v+tb.v,tot)); the     } -LL ans =0 ; in     intHead =0 ;  the     intTail =0 ;  theDeep[tot] =0 ;  About      for(que[tail++] = tot; head < tail; head + +) the     { the         //printf ("%d%d%d\n", que[head],ch[que[head]][0],ch[que[head]][1]); the         //:p rintf ("%d\n", Deep[que[head]]); +         if(ch[que[head]][0]){ -Que[tail] = ch[que[head]][0]; thedeep[ch[que[head]][0]] = Deep[que[head]] +1;BayiTail + + ;  the         } the         if(ch[que[head]][1]){ -Que[tail] = ch[que[head]][1]; -deep[ch[que[head]][1]] = Deep[que[head]] +1; theTail + + ;  the         } the     } the      for(inti =1; I <= N;i + +) -Ans + = 1LL * Deep[i] *A[i]; theprintf"%i64d\n", ans); the return 0; the}`
View Code

ASC2 C Hyperhuffman Priority queue

Related Keywords:
Related Article

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.