[挑战程序设计竞赛] POJ 3253 - Fence Repair

哈夫曼树裸题,用优先队列做的。。

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
typedef long long LL;
const int INF = 500000001;
const double EPS = 1e-9;
const double PI = acos(-1.0);
using namespace std;
struct ak
{
    int x;
    bool operator <(const ak & a)const
    {
        return a.x < x;
    }
}temp;
int main()
{
    //freopen("test0.in", "r", stdin);
    //freopen("test0.out", "w", stdout);
    //srand(time(NULL));
    long long ans;
    int N, a[20005];
    while(~scanf("%d", &N))
    {
        priority_queue pq;
        for(int i = 0; i < N; ++i)
        {
            scanf("%d", &temp.x);
            pq.push(temp);
        }
        ans = 0;
        while(pq.size() != 1)
        {
            temp.x = pq.top().x;
            pq.pop();
            temp.x += pq.top().x;
            pq.pop();
            ans += temp.x;
            pq.push(temp);
        }
        printf("%lld\n", ans);
    }
    return 0;
}


你可能感兴趣的:(STL,POJ,挑战程序设计竞赛)