poj 3253 Fence Repair(优先队列+哈夫曼树)
题目地址: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(优先队列+哈夫曼树)的更多相关文章
- POJ 3253 Fence Repair(哈夫曼树)
Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 26167 Accepted: 8459 Des ...
- poj 3253 Fence Repair (水哈夫曼树)
题目链接: http://poj.org/problem?id=3253 题目大意: 有一根木棍,需要截成n节,每节都有固定的长度,一根长度为x的木棒结成两段,需要花费为x,问截成需要的状态需要最小的 ...
- POJ 3253 Fence Repair (哈夫曼树)
Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 19660 Accepted: 6236 Des ...
- poj 3253 Fence Repair (优先队列,哈弗曼)
题目链接:http://poj.org/problem?id=3253 题意:给出n块木板的长度L1,L2...Ln,求在一块总长为这个木板和的大木板中如何切割出这n块木板花费最少,花费就是将木板切割 ...
- poj 3253 Fence Repair 优先队列
poj 3253 Fence Repair 优先队列 Description Farmer John wants to repair a small length of the fence aroun ...
- poj3253 Fence Repair【哈夫曼树+优先队列】
Description Farmer John wants to repair a small length of the fence around the pasture. He measures ...
- POJ - 3253 Fence Repair 优先队列+贪心
Fence Repair Farmer John wants to repair a small length of the fence around the pasture. He measures ...
- poj 3253 Fence Repair(优先队列+huffman树)
一个很长的英文背景,其他不说了,就是告诉你锯一个长度为多少的木板就要花多少的零钱,把一块足够长(不是无限长)的木板锯成n段,每段长度都告诉你了,让你求最小花费. 明显的huffman树,优先队列是个很 ...
- POJ 3253 Fence Repair (优先队列)
POJ 3253 Fence Repair (优先队列) Farmer John wants to repair a small length of the fence around the past ...
- POJ 3253 Fence Repair(修篱笆)
POJ 3253 Fence Repair(修篱笆) Time Limit: 2000MS Memory Limit: 65536K [Description] [题目描述] Farmer Joh ...
随机推荐
- 李洪强iOS开发之-PCH文件的配置
pch 可以用来存储共享信息,比如设备屏幕的宽度,高度.版本号等等 公用信息 Xcode 老版本会自动为我们创建pch文件,新版本开始不自动创建了,如果需要使用可以自己手动创建 创建完成后可以在里面定 ...
- [codility]PrefixMaxProduct
Codility Certificate题目.求product最大值,product为长度*出现次数,例子"abababa"如下: "a", whose pro ...
- Show username instead of "System Account" in SharePoint 2010
Problems: When I load my local SharePoint site, the account always show as "System Account" ...
- .Net remoting, Webservice,WCF,Socket区别
传统上,我们把计算机后台程序(Daemon)提供的功能,称为"服务"(service).比如,让一个杀毒软件在后台运行,它会自动监控系统,那么这种自动监控就是一个"服务& ...
- Codevs_1166_[NOIP2007]_矩阵取数游戏_(动态规划+高精度)
描述 http://codevs.cn/problem/1166/ 分析 #include <iostream> #include <cstring> #include < ...
- -_-#【减少 DOM 元素】textarea, script 延迟渲染
淘宝详情页的 BigRender 优化与存放大块 HTML 内容的最佳方式 淘宝详情页的BigRender优化的最佳方式 <!DOCTYPE html> <html> < ...
- MySql定位执行效率较低的SQL语句
MySQL能够记录执行时间超过参数 long_query_time 设置值的SQL语句,默认是不记录的. 获得初始锁定的时间不算作执行时间.mysqld在SQL执行完和所有的锁都被释放后才写入日志.且 ...
- SQL server 2012序列号 注册码 z
MICROSOFT SQL SERVER 2012 企业核心版激活码序列号: FH666-Y346V-7XFQ3-V69JM-RHW28 MICROSOFT SQL SERVER 2012 商业智能版 ...
- 【CSS】Intermediate6:Display
1display :inline/block/none 2.inline value Cause all list items in a list to appear next to each oth ...
- lru cache java
http://www.acmerblog.com/leetcode-lru-cache-lru-5745.html acm之家的讲解是在是好,丰富 import java.util.LinkedHas ...