SDUT2127树-堆结构练习——合并果子之哈夫曼树

来源:互联网 发布:ss账号哪里购买好 知乎 编辑:程序博客网 时间:2024/05/02 00:08
#include<bits/stdc++.h>using namespace std;int main(){    int n;    scanf("%d",&n);    priority_queue<int,vector<int>,greater<int> > q;    int cou[150],lhf=0;    memset(cou,0,sizeof(cou));    while(n--)    {        int t;        scanf("%d",&t);        q.push(t);    }    while(!q.empty())    {        int m=q.top();        q.pop();        if(!q.empty())        {            int n=q.top();            q.pop();            int p=m+n;            lhf+=p;            q.push(p);        }    }    printf("%d",lhf);}

0 0
原创粉丝点击