标签:style blog color os strong io
竟然做过原题,一眼看上去竟然没感觉。。。
哈夫曼树定义:给定n个权值作为n个叶子结点,构造一棵二叉树,若带权路径长度达到最小,称这样的二叉树为最优二叉树,也称为哈夫曼树(Huffman tree)。哈夫曼树是带权路径长度最短的树,权值较大的结点离根较近。
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#pragma comment(linker, "/STACK:1024000000");
#define EPS (1e-8)
#define LL long long
#define ULL unsigned long long LL
#define _LL __LL64
#define _INF 0x3f3f3f3f
#define Mod 1000000007
#define LM(a,b) (((ULL)(a))<<(b))
#define RM(a,b) (((ULL)(a))>>(b))
using namespace std;
struct N
{
LL ans;
bool operator < (const N &a) const{
return a.ans < ans;
}
};
priority_queue<N> q;
int main()
{
int n;
N f,t;
while(q.empty() == false)
q.pop();
while(scanf("%d",&n) != EOF)
{
LL sum = 0;
while(n--)
{
scanf("%lld",&f.ans);
q.push(f);
}
while(q.empty() == false)
{
f = q.top();
q.pop();
if(q.empty() == false)
{
t = q.top();
q.pop();
f.ans += t.ans;
sum += f.ans;
q.push(f);
}
}
printf("%lld\n",sum);
}
return 0;
}
哈夫曼树 POJ 3253 Fence Repair,码迷,mamicode.com
标签:style blog color os strong io
原文地址:http://blog.csdn.net/zmx354/article/details/24840453