懒散的小明 STL 优先队列实现 数据结构中的哈弗曼

懒惰的小明 STL 优先队列实现 数据结构中的哈弗曼
懒省事的小明
时间限制:3000 ms  |           内存限制:65535 KB
难度:3
描述
      小明很想吃果子,正好果园果子熟了。在果园里,小明已经将所有的果子打了下来,而且按果子的不同种类分成了不同的堆。小明决定把所有的果子合成一堆。 因为小明比较懒,为了省力气,小明开始想点子了:
  每一次合并,小明可以把两堆果子合并到一起,消耗的体力等于两堆果子的重量之和。可以看出,所有的果子经过n-1次合并之后,就只剩下一堆了。小明在合并果子时总共消耗的体力等于每次合并所耗体力之和。
  因为还要花大力气把这些果子搬回家,所以小明在合并果子时要尽可能地节省体力。假定每个果子重量都为1,并且已知果子的种类数和每种果子的数目,你的任务是设计出合并的次序方案,使小明耗费的体力最少,并输出这个最小的体力耗费值。
  例如有3种果子,数目依次为1,2,9。可以先将1、2堆合并,新堆数目为3,耗费体力为3。接着,将新堆与原先的第三堆合并,又得到新的堆,数目为12,耗费体力为12。所以小明总共耗费体力=3+12=15。可以证明15为最小的体力耗费值。
输入
第一行输入整数N(0<N<=10)表示测试数据组数。接下来每组测试数据输入包括两行,第一行是一个整数n(1<=n<=12000),表示果子的种类数。第二行包含n个整数,用空格分隔,第i个整数ai(1<=ai<=20000)是第i种果子的数目。
输出
每组测试数据输出包括一行,这一行只包含一个整数,也就是最小的体力耗费值。
样例输入
1
3 
1 2 9
样例输出
15
看题就知道 哈弗曼
懒散的小明    STL   优先队列实现      数据结构中的哈弗曼
#include<cstdio>
#include<set>
using namespace std;
int main()
{
    int n;
    multiset<int> set1;
    while(scanf("%d",&n)!=EOF)
    {
        while(n--)
        {
            set1.clear();
            int m;
            scanf("%d",&m);
            for(int i=0;i<m;i++)
            {
                int  a;
                scanf("%d",&a);
                set1.insert(a);
            }
            long long sum=0;
            while(set1.size()!=1)
            {
                int a=*set1.begin();
                set1.erase(set1.begin());
                int b=*set1.begin();
                set1.erase(set1.begin());
                int c=a+b;
                sum+=c;
                set1.insert(c);
            }
            printf("%lld\n",sum);
        }
    }
    return 0;
}

#include<iostream>
#include<algorithm>
#include<queue>
#define N 100

using namespace std;

int main()
{
    long long  test,n,i,t,tt,ans;
    priority_queue<long long, vector<long long >, greater<long long> >pq;
    cin>>test;
    while(test--)
    {
        while(!pq.empty()) pq.pop();
        cin>>n;
        for(i=0; i<n; i++)
        {
            cin>>t;
            pq.push(t);
        }
        ans=0;
        while(!pq.empty())
        {
            t=pq.top();
            pq.pop();
            if(pq.empty())
            {
                break;
            }
            else
            {
                tt=pq.top();
                pq.pop();
                tt=tt+t;
                ans+=tt;
                pq.push(tt);
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}

#include<iostream>
#include<algorithm>
#include<queue>
#define N 100

using namespace std;

int main()
{
    long long  test,n,i,t,tt,ans;
    priority_queue<long long, vector<long long >, greater<long long> >pq;
    cin>>test;
    while(test--)
    {
        while(!pq.empty()) pq.pop();
        cin>>n;
        for(i=0; i<n; i++)
        {
            cin>>t;
            pq.push(t);
        }
        ans=0;
        while(!pq.empty())
        {
            t=pq.top();
            pq.pop();
            if(pq.empty())
            {
                break;
            }
            else
            {
                tt=pq.top();
                pq.pop();
                tt=tt+t;
                ans+=tt;
                pq.push(tt);
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}