题目地址: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. Android 发送HTTP GET POST 请求以及通过 MultipartEntityBuilder 上传文件

    折腾了好几天的 HTTP 终于搞定了,经测试正常,不过是初步用例测试用的,因为后面还要修改先把当前版本保存在博客里吧. 其中POST因为涉及多段上传需要导入两个包文件,我用的是最新的 httpmine ...

  2. [OJ] Single Number II

    LintCode 83. Single Number II (Medium) LeetCode 137. Single Number II (Medium) 以下算法的复杂度都是: 时间复杂度: O( ...

  3. AfxBeginThread和AfxEndThread+内存泄露

    ref http://blog.csdn.net/kut00/article/details/4209680 启动线程: CWinThread* AfxBeginThread( 线程函数, this ...

  4. POJ_3273_Monthly_Expense_(二分,最小化最大值)

    描述 http://poj.org/problem?id=3273 共n个月,给出每个月的开销.将n个月划分成m个时间段,求m个时间段中开销最大的时间段的最小开销值. Monthly Expense ...

  5. WordPress FunCaptcha插件跨站脚本漏洞

    漏洞名称: WordPress FunCaptcha插件跨站脚本漏洞 CNNVD编号: CNNVD-201311-431 发布时间: 2013-11-29 更新时间: 2013-11-29 危害等级: ...

  6. [转] ICPC2013 World Finals赛后感

    原文地址:http://blog.sina.com.cn/s/blog_6c7729450101lmll.html Orz... 26号考完最后一科计网,27号准备了一下各种材料,28号凌晨就踏上旅程 ...

  7. DD_belatedPNG,IE6下PNG透明解决方案

    我们知道IE6是不支持透明的PNG的,这无疑限制了网页设计的发挥空间. 然而整个互联网上解决这个IE6的透明PNG的方案也是多不胜数,从使用IE特有的滤镜或是e­xpression,再到javascr ...

  8. 通过 Azure 媒体管理门户开始使用直播流媒体

    Jason Suess Azure媒体服务首席项目经理 几个月前,我们宣布发布 Azure媒体服务直播服务的公共预览版.其实这些直播服务早已被美国国家广播公司体育台用于多项重大体育赛事的多平台直播 ...

  9. epub3 in action: epub3文件格式简介

    epub3文件就是一个符合epub3标准,以epub为扩展名的zip压缩文件.epub3标准则是基于html5.css3.svg等web标准以及mathML等来展示内容.下图就是一个简单epub3文件 ...

  10. haporoxy的keeplaive ZZ

    转载一个别人总结的http://hi.baidu.com/%D3%E3%B5%C1%B5%C1/blog/item/04ffd1b1854f69ef30add1e8.html httpclose, h ...