POJ3253 Haffman

POJ3253

分析:

简单的哈弗曼树的应用。

AC代码:

 1 //Memory: 316K        Time: 16MS

 2 #include <iostream>

 3 #include <cstring>

 4 #include <cstdio>

 5 #include <queue>

 6 

 7 using namespace std;

 8 

 9 int n;

10 int l;

11 priority_queue <int, vector<int>, greater<int>> q;

12 

13 int main()

14 {

15     while ( !q.empty() ) q.pop();

16     scanf("%d", &n);

17     for (int i = 0; i < n; i++) {

18         scanf("%d", &l);

19         q.push(l);

20     }

21     long long ans = 0;

22     while ( q.size() >= 2 ) {

23         int a = q.top();

24         q.pop();

25         int b = q.top();

26         q.pop();

27         ans += (a + b);

28         q.push(a + b);

29     }

30     printf("%lld\n", ans);

31     return 0;

32 }

 

你可能感兴趣的:(poj)