poj 3253 Fence Repair(优先队列+哈夫曼树)
来源:互联网 发布:犀牛软件分解模型 编辑:程序博客网 时间:2024/06/07 06:52
题目地址: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;}
2 1
- POJ 3253 Fence Repair(优先队列&哈夫曼树)
- POJ 3253 Fence Repair 哈夫曼树 优先队列
- poj 3253 Fence Repair【哈夫曼树、优先队列】
- POJ 3253 Fence Repair (优先队列)
- POJ 3253 Fence Repair(优先队列)
- POJ 3253 Fence Repair(优先队列)
- 【POJ】-3253-Fence Repair(优先队列)
- POJ:3253 Fence Repair(优先队列)
- Fence Repair(优先队列) POJ 3253
- poj 3253 Fence Repair(优先队列+哈夫曼树)
- POJ 3253 Fence Repair 题解(优先队列实现哈夫曼树)
- poj-3253-Fence Repair-优先队列(哈夫曼树)
- POJ 3253-Fence Repair(哈夫曼树-最小值优先队列)
- POJ 3253 Fence Repair(优先队列构造哈夫曼树)
- POJ 3253 Fence Repair(哈夫曼树+优先队列)
- poj 3253 Fence Repair 优先队列维护
- POJ 3253 优先队列 Fence Repair
- POJ 3253 Fence Repair (贪心&优先队列)
- ZOJ 3795 Grouping 强联通+偏序集
- Ubuntu下单网卡多IP地址的配置
- VC+matlab通过Com接口实现混合编程
- shell 下正则表达式的匹配
- socket image
- poj 3253 Fence Repair(优先队列+哈夫曼树)
- BC-Round3 hdu4907
- ehcache memcache redis 三大缓存男高音
- hdoj.4883 TIANKENG’s restaurant 20140804
- 关于 Error reading tld listeners java.lang.NullPointerException解决方法
- PhoneGap 中navigator.notification.confirm 的用法详解
- ZCA白化变换推导——Learning Multiple Layers of Features from Tiny Images
- 版本控制-SVN版本库的搭建方式
- RTSP 协议分析 (一)