哈夫曼树

使用优先队列的哈夫曼树

#include<iostream>
#include<stdio.h>
#include <vector>
#include<queue>
using namespace std;
priority_queue<long long, vector<long long>, greater<long long>>q;
//哈夫曼树
int main()
{
	int n;
	long long temp, x, y, ans = 0;
	scanf("%d", &n);
	for (int i = 0; i < n; i++)
	{
		scanf("%lld", &temp);
		q.push(temp);
	}
	while (q.size() > 1)
	{
		x = q.top();
		q.pop();
		y = q.top();
		q.pop();
		q.push(x + y);
		ans = x + y;
	}
	printf("%lld", ans);
	return 0;
}
原文地址:https://www.cnblogs.com/code-fun/p/15228768.html