UVa 10954 (Huffman 优先队列) Add All

直接用一个优先队列去模拟Huffman树的建立过程。

每次取优先队列前两个数,然后累加其和,把这个和在放入到优先队列中去。

 #include <cstdio>
#include <queue>
using namespace std; int main()
{
int n;
while(scanf("%d", &n) == && n)
{
priority_queue<int, vector<int>, greater<int> > q;
int x, ans = ;
for(int i = ; i < n; i++) { scanf("%d", &x); q.push(x); }
for(int i = ; i < n-; i++)
{
int a = q.top(); q.pop();
int b = q.top(); q.pop();
ans += a + b;
q.push(a + b);
}
printf("%d\n", ans);
} return ;
}

代码君

上一篇:基于SMTP协议的CMD命令邮件发送


下一篇:link和@import的区别