Hyperhuffman (哈夫曼树(水题))

来源:互联网 发布:网络翻译兼职 编辑:程序博客网 时间:2024/04/30 07:13

题目来源:https://cn.vjudge.net/problem/ZOJ-2339
【题意】
整篇文章都在解释什么是哈夫曼树,怎么用,用于什么,怎么构建,然后就给出频率,让算出整棵(最优二叉树)哈夫曼树的权值。
【思路】
首先给出一个博客地址,能够清楚的了解什么是哈夫曼树:http://www.cnblogs.com/wuyuankun/p/3982216.html。
然后就可以把这道题A了。
【代码】

#include<map>#include<stack>#include<queue>#include<cstdio>#include<algorithm>#include<cstring>#include<cmath>#include<iostream>#include<string>#define mem(a,b) memset(a,b,sizeof(a))using namespace std;typedef long long LL;const int INF=0x3f3f3f3f;int main(){    int t;    scanf("%d",&t);    priority_queue<LL,vector<LL>,greater<LL> > q;    while(t--)    {        int n,s;        while(!q.empty())            q.pop();        scanf("%d",&n);        while(n--)        {            scanf("%d",&s);            q.push(s);        }        LL ans=0,x,y;        while(!q.empty())        {            x=q.top();            q.pop();            if(q.empty())            break;            y=q.top();            q.pop();            ans+=x+y;            q.push(x+y);        }        printf("%lld\n",ans);        if(t!=0)            printf("\n");    }}
0 0