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 ...
随机推荐
- RecyclerView一个奇怪的npe异常
java.lang.NullPointerException at android.support.v7.widget.RecyclerView.computeVerticalScrollOffset ...
- 【HDOJ】3016 Man Down
线段树+spfa求最长路.逆向思维,从最底下一块板子建图.需要注意的是任何一个板子掉落下面再无板子,此时都可以看做一个终结状态. /* 3016 */ #include <iostream> ...
- 【HDOJ】2062 Subset sequence
这道题目非常好,饶了点儿圈子.我的思路是,先按照组排列.例如,1 2 31 2 2 1 3 11 2 3 2 1 3 ...
- FRM-40401 No changes to save error
FAQ: I have updated a table from the form and written a update statement in the when-button-pressed ...
- 获取Mac、CPUID、硬盘序列号、本地IP地址、外网IP地址OCX控件
提供获取Mac.CPUID.硬盘序列号.本地IP地址.外网IP地址OCX控件 开发语言:vc++ 可应用与WEB程序开发应用 <HTML><HEAD><TITLE> ...
- 转自 Because of you 的总结
上下界网络流的问题严格的分,可以分为四类吧. 1:无源汇可行流 sgu 194 2:有源汇可行流 poj 2396 这题比较好,我建图建了将近200行 3:有源汇最大流 zoj 3496 这 ...
- c程序设计语言_习题1-11_学习单元测试,自己生成测试输入文件
How would you test the word count program? What kinds of input are most likely to uncover bugs if th ...
- SharePoint 2010 master page 控件介绍(2):ribbon (一同事读听着像泪奔)
转:http://blog.csdn.net/lgm97/article/details/6409208 <!-- ===== 开始Ribbon ======================= ...
- 两种应该掌握的排序方法--------1.shell Sort
先了解下什么都有什么排序算法 https://en.wikipedia.org/wiki/Sorting_algorithm http://zh.wikipedia.org/zh/%E6%8E%92% ...
- Zookeeper的一致性协议:Zab(转)
Zookeeper使用了一种称为Zab(Zookeeper Atomic Broadcast)的协议作为其一致性复制的核心,据其作者说这是一种新发算法,其特点是充分考虑了Yahoo的具体情况:高吞吐量 ...