题目地址:POJ 3253

哈夫曼树的结构就是一个二叉树,每个父节点都是两个子节点的和。

这个题就是能够从子节点向根节点推。

每次选择两个最小的进行合并。将合并后的值继续加进优先队列中。直至还剩下一个元素为止。

代码例如以下:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <queue>
#include <map>
#include<algorithm> using namespace std;
struct node
{
__int64 x;
bool operator <(const node &a) const
{
return x>a.x;
}
};
int main()
{
__int64 n, i, j, x, ans;
while(scanf("%I64d",&n)!=EOF)
{
ans=0;
priority_queue<node>q;
node p, f1, f2, s;
for(i=0; i<n; i++)
{
scanf("%I64d",&p.x);
q.push(p);
}
while(!q.empty())
{
f1=q.top();
q.pop();
if(q.empty())
{
break;
}
f2=q.top();
q.pop();
s.x=f1.x+f2.x;
q.push(s);
//printf("%d %d\n",f1.x,f2.x);
ans+=s.x;
}
printf("%I64d\n",ans);
}
return 0;
}

poj 3253 Fence Repair(优先队列+哈夫曼树)的更多相关文章

  1. POJ 3253 Fence Repair(哈夫曼树)

    Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 26167   Accepted: 8459 Des ...

  2. poj 3253 Fence Repair (水哈夫曼树)

    题目链接: http://poj.org/problem?id=3253 题目大意: 有一根木棍,需要截成n节,每节都有固定的长度,一根长度为x的木棒结成两段,需要花费为x,问截成需要的状态需要最小的 ...

  3. POJ 3253 Fence Repair (哈夫曼树)

    Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 19660   Accepted: 6236 Des ...

  4. poj 3253 Fence Repair (优先队列,哈弗曼)

    题目链接:http://poj.org/problem?id=3253 题意:给出n块木板的长度L1,L2...Ln,求在一块总长为这个木板和的大木板中如何切割出这n块木板花费最少,花费就是将木板切割 ...

  5. poj 3253 Fence Repair 优先队列

    poj 3253 Fence Repair 优先队列 Description Farmer John wants to repair a small length of the fence aroun ...

  6. poj3253 Fence Repair【哈夫曼树+优先队列】

    Description Farmer John wants to repair a small length of the fence around the pasture. He measures ...

  7. POJ - 3253 Fence Repair 优先队列+贪心

    Fence Repair Farmer John wants to repair a small length of the fence around the pasture. He measures ...

  8. poj 3253 Fence Repair(优先队列+huffman树)

    一个很长的英文背景,其他不说了,就是告诉你锯一个长度为多少的木板就要花多少的零钱,把一块足够长(不是无限长)的木板锯成n段,每段长度都告诉你了,让你求最小花费. 明显的huffman树,优先队列是个很 ...

  9. POJ 3253 Fence Repair (优先队列)

    POJ 3253 Fence Repair (优先队列) Farmer John wants to repair a small length of the fence around the past ...

  10. POJ 3253 Fence Repair(修篱笆)

    POJ 3253 Fence Repair(修篱笆) Time Limit: 2000MS   Memory Limit: 65536K [Description] [题目描述] Farmer Joh ...

随机推荐

  1. SQLite入门与分析(八)---存储模型(3)

    写在前面:接上一节,本节主要讨论索引页面格式,以及索引与查询优化的关系. (1)索引页面格式sqlite> select * from sqlite_master;table|episodes| ...

  2. LINUX下安装PHP(CGI模式)和NGINX[转]

    安装所需依赖 yum -y install gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freety ...

  3. More on 1Password’s Components

    Stefan van As of 1Password fame sent me a more exhaustive list of the libraries and tools used in 1P ...

  4. KXFW界面库

    如果你看到这里,觉得这个UI库不错,那请你一定要记住如下这些话.这个开源已经有更好的替代者,那就是QML,无法否认QML会有一些性能或BUG的问题,但也无法否定它的实现机制是非常棒的,你完全可以利用它 ...

  5. java中遍历List中的map问题

    List list = new ArrayList();Map map = null; while (rs.next()) { map = new HashMap(); map.put("f ...

  6. JNI编程(一) —— 编写一个最简单的JNI程序

    来自:http://chnic.iteye.com/blog/198745 忙了好一段时间,总算得了几天的空闲.貌似很久没更新blog了,实在罪过.其实之前一直想把JNI的相关东西整理一下的,就从今天 ...

  7. TigerLeapMC V1.3 for Windows(支持DLNA)

    TigerLeapMC V1.3 2014-04-10: 1.更新tlplayer TigerLeapMC是基于tlplayer作为播放器的集成DLNA,(DMS,DMR,DMP)等,支持各种网络播放 ...

  8. 更新你的jar包

    jar文件:/home/resin.jar需更新包中com/caucho/server/port/Port.class类文件 方法1:jar uf resin.jar com/caucho/serve ...

  9. BrandZ:2016年全球最具价值品牌百强榜(完整报告)

    https://wppbaz.com/admin/uploads/files/BZ_Global_2016_Report.pdf Millward Brown编制的BrandZ最新排行榜(2016 B ...

  10. SharePoint 2010 Ribbon的实现

    转:http://blog.csdn.net/wang4237/article/details/5306335 SharePoint2010的页面风格发生了很大的改变,其页面风格类似于Office的视 ...