哈夫曼,优先队列的使用:
#include<iostream>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
typedef long long ll;
const ll mod=1e5+10;
const ll maxx=20005;
struct node{
ll cost;
friend bool operator<(const node a,const node b){
return a.cost>b.cost;
}
};
ll cmp(node a,node b){
return a.cost<b.cost;
}
int main(){
ll n;
while(cin>>n){
priority_queue<node>a;
while(!a.empty()){
a.pop();
}
for(int i=0;i<n;i++){
node x;
cin>>x.cost;
a.push(x);
}
ll mincost=0;
while(a.size()>1){
node u=a.top();
a.pop();
node v=a.top();
a.pop();
ll sumcost=u.cost+v.cost;
node temp;
temp.cost=sumcost;
a.push(temp);
mincost+=sumcost;
}
cout<<mincost<<endl;
}
return 0;
}