哈弗曼树

来源:互联网 发布:剑灵金燕捏脸数据 编辑:程序博客网 时间:2024/05/29 10:00

#include<queue>#include<functional>//如果在VS环境下,必须加上这一个头文件,因为greater模板在里边#include<stdio.h>using namespace std;priority_queue< int , vector<int> ,greater<int> > Q;//建立一个大顶堆int main(){int n,x;scanf("%d",&n);while(Q.empty()==false)for(int i=0;i<n;i++){scanf("%d",&x);Q.push(x);}int ans=0;while(Q.size()>1){int a=Q.top();Q.pop();int b=Q.top();Q.pop();ans+=a+b;Q.push(a+b);}printf("%d\n",ans);system("pause");return 0;}



0 0