哈夫曼算法,水的一匹
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;
int a[
5000+
10];
int n;
priority_queue<
int ,vector<
int>,greater<
int> >
aa;
int main()
{
while(~scanf(
"%d",&n)&&
n)
{ while(aa.size())
aa.pop();
for(
int i=
0;i<n;i++
)
{
scanf("%d",&
a[i]);
aa.push(a[i]);
}
int ans=
0;
while(aa.size()>
1)
{
int x=
aa.top(); aa.pop();
int y=
aa.top(); aa.pop();
ans=x+y+
ans;
aa.push(x+
y);
}
printf("%d\n",ans);
}
return 0;
}
转载于:https://www.cnblogs.com/Wangwanxiang/p/6876044.html
相关资源:数据结构—成绩单生成器
转载请注明原文地址: https://win8.8miu.com/read-1495863.html