poj 3253
哈夫曼树,挺有意思的~~~
1 #include <iostream> 2 #include <string> 3 #include <vector> 4 #include <cstdlib> 5 #include <cmath> 6 #include <map> 7 #include <algorithm> 8 #include <list> 9 #include <ctime> 10 #include <set> 11 #include <string.h> 12 #include <queue> 13 #include <cstdio> 14 using namespace std; 15 #define CLR(arr, what) memset(arr, what, sizeof(arr)) 16 typedef long long ll; 17 int main() { 18 int N, t, a, b; 19 scanf("%d", &N); 20 priority_queue<ll, vector<ll>, greater<ll> > mq; 21 for (int i = 0; i < N; i++) { 22 scanf("%ld", &t); 23 mq.push(t); 24 } 25 ll res = 0; 26 while (!mq.empty()) { 27 a = mq.top(); 28 mq.pop(); 29 if (mq.empty()) { 30 break; 31 } else { 32 b = mq.top(); 33 mq.pop(); 34 res = res + a + b; 35 mq.push(a + b); 36 } 37 38 } 39 cout << res << endl; 40 return 0; 41 }