** 2193 - 哈夫曼树 ---特殊二叉树
** 来源: 东方博宜oj oj.czos.cn
*叶子节点权值*长度之和==非叶子节点值之和
#include<bits/stdc++.h>
using namespace std;
// 思路:使用优先队列模拟哈夫曼树的WPL过程
// 小根堆
priority_queue<int,vector<int>,greater<int> > q;
int n,x,ans=0; // ans:是哈夫曼树的WPL值
int main()
{
cin>>n;
while(n--)
{
cin>>x;
q.push(x);
}
int a,b;
//当队列中超过1个元素
while(q.size()>1)
{
//获取队列头部的2个最小的元素
a=q.top();
q.pop();
b=q.top();
q.pop();
ans+=a+b;
q.push(a+b);
}
cout<<ans;
return 0;
}
2193 - 哈夫曼树 ---特殊二叉树
于 2023-02-17 13:57:51 首次发布