Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=5350
IQ questions ... To find the minimum Huffman tree encoding. Take the smallest two numbers in the data, add them to the sum, and add them as a node.
Use the priority queue.
Like Case2, the smallest Huffman tree is like this.
104 ///// - - / 1
Add the values of all non-leaf nodes.
1#include <algorithm>2#include <iostream>3#include <iomanip>4#include <cstring>5#include <climits>6#include <complex>7#include <fstream>8#include <cassert>9#include <cstdio>Ten#include <bitset> One#include <vector> A#include <deque> -#include <queue> -#include <stack> the#include <ctime> -#include <Set> -#include <map> -#include <cmath> + - using namespacestd; + Apriority_queue<int, vector<int, greater<int> >PQ; at - intMain () { - //freopen ("in", "R", stdin); - intT, N; - inttmp; -scanf"%d", &T); in while(t--) { - while(!Pq.empty ()) { to Pq.pop (); + } -scanf"%d", &n); the for(inti =0; I < n; i++) { *scanf"%d", &tmp); $ Pq.push (TMP);Panax Notoginseng } - Long LongAns =0; the intA, B; + while(n-->1) { AA =pq.top (); the Pq.pop (); +b =pq.top (); - Pq.pop (); $TMP = a +b; $ Pq.push (TMP); -Ans + =tmp; - } theprintf"%i64d\n", ans); - }Wuyi}
1#include <algorithm>2#include <iostream>3#include <iomanip>4#include <cstring>5#include <climits>6#include <complex>7#include <fstream>8#include <cassert>9#include <cstdio>Ten#include <bitset> One#include <vector> A#include <deque> -#include <queue> -#include <stack> the#include <ctime> -#include <Set> -#include <map> -#include <cmath> + - using namespacestd; + Apriority_queue<int, vector<int, greater<int> >PQ; at - intMain () { - //freopen ("in", "R", stdin); - intT, N; - inttmp; -scanf"%d", &T); in while(t--) { - while(!Pq.empty ()) { to Pq.pop (); + } -scanf"%d", &n); the for(inti =0; I < n; i++) { *scanf"%d", &tmp); $ Pq.push (TMP);Panax Notoginseng } - Long LongAns =0; the intA, B; + while(n-->1) { AA =pq.top (); the Pq.pop (); +b =pq.top (); - Pq.pop (); $TMP = a +b; $ Pq.push (TMP); -Ans + =tmp; - } theprintf"%i64d\n", ans); - }Wuyi}
[HDOJ5350] MZL ' s munhaff function