POJ3253 Haffman

时间:2021-07-04 17:13:49

POJ3253

分析:

简单的哈弗曼树的应用。

AC代码:

 //Memory: 316K        Time: 16MS
 #include <iostream>
 #include <cstring>
 #include <cstdio>
 #include <queue>

 using namespace std;

 int n;
 int l;
 priority_queue <int, vector<int>, greater<int>> q;

 int main()
 {
     while ( !q.empty() ) q.pop();
     scanf("%d", &n);
     ; i < n; i++) {
         scanf("%d", &l);
         q.push(l);
     }
     ;
      ) {
         int a = q.top();
         q.pop();
         int b = q.top();
         q.pop();
         ans += (a + b);
         q.push(a + b);
     }
     printf("%lld\n", ans);
     ;
 }