poj 3253 Fence Repair哈夫曼树

#include <iostream>
#include <cstdio>
#include <queue>
using namespace std;
struct node{
    int x;
    bool operator < (const node &a) const{
        return a.x < x ;
    }
};

int main(){
	//freopen("in.txt","r",stdin);
    priority_queue<node> q;
    int n, temp;
    long long sum = 0;
    cin>>n;
    while(n--){
        cin>>temp;
        node n1;
        n1.x= temp;
        q.push(n1);
    }
    if(q.size() == 1)
        sum += q.top().x;
    while(q.size() > 1){
        int a = q.top().x; q.pop();
        int b = q.top().x; q.pop();
        sum = sum + a + b;
        node n2;
        n2.x = a + b;
        q.push(n2);
    }
    q.pop();
    cout<<sum<<endl;
	return 0;
}


你可能感兴趣的:(哈夫曼树)