用哈夫曼树求解。

#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<list>
#include<iomanip>
#include<fstream>
using namespace std;
template<class T>
class Queue {private:list<T>queue;
public:Queue() {}~Queue() {}int Empty() { return queue.empty(); }int Size() { return queue.size(); }void Push(const T& item) { queue.push_back(item); }T Pop() { T item = queue.front(); queue.pop_front(); return item; }void Clear() { queue.clear(); }
};
template<class T>
struct BTNode {T data;BTNode* left, * right;BTNode(const T& item = T(), BTNode* lp = NULL, BTNode* rp = NULL) :data(item), left(lp), right(rp) {}
};
template<class T>
BTNode<T>* GetNode(const T& item, BTNode<T>* lp = NULL, BTNode<T>* rp = NULL){BTNode<T>* p;p = new BTNode<T>(item, lp, rp);if (p == NULL){cout << "the new is faliure" << endl;exit(1);}return p;
}
template<class T>
class Heap {private:vector<T>vec;int size;void BuildHeap();void PercolateDown(int h);void PercolateUp();
public:explicit Heap(int max = 100) :vec(max), size(0) {}explicit Heap(const vector<T>& vt);bool Empty()const{return size == 0;}int Size() {return size;}void Insert(const T& item);const T& Top()const {return vec[0];}void Deletemin();void Deletemin(T& item);void Clear() {size = 0;}
};
template<class T>
Heap<T>::Heap(const vector<T>& vt) :vec(vt.size() + 10), size(vt.size()){for (int i = 0; i < size; i++){vec[i] = vt[i];}BuildHeap();
}
template<class T>
void Heap<T>::BuildHeap(){for (int i = size / 2 - 1; i >= 0; i--){PercolateDown(i);}
}
template<class T>
void Heap<T>::PercolateDown(int h){int p = h, c = 2 * p + 1;T temp = vec[p];while (c < size){if (c + 1 < size && vec[c + 1] < vec[c]){c++;}if (temp <= vec[c]){break;}else{vec[p] = vec[c];p = c;c = 2 * p + 1;}}vec[p] = temp;
}
template<class T>
void Heap<T>::PercolateUp(){int c = size - 1, p = (c - 1) / 2;T temp = vec[c];while (c > 0){if (temp >= vec[p]) {break;}else{vec[c] = vec[p];c = p;p = (c - 1) / 2;}}vec[c] = temp;
}
template<class T>
void Heap<T>::Insert(const T& item){if (size == vec.size()){vec.resize(vec.size() * 2);}vec[size++] = item;PercolateUp();
}template<class T>
void Heap<T>::Deletemin(){if (size <= 0)return;vec[0] = vec[size - 1];size--;PercolateDown(0);
}
template<class T>
void Heap<T>::Deletemin(T& item){if (size <= 0)return;item = vec[0];vec[0] = vec[size - 1];size--;PercolateDown(0);
}
template<class T>
void BuildHeapMax(T* pa, int size){for (int i = size / 2 - 1; i >= 0; i--){PercolateDown(pa, size);}
}
template<class T>
struct HufmNode {BTNode<T>* t;int operator<(const HufmNode& h) {return (t->data < h.t->data);}int operator<=(const HufmNode& h) {return (t->data <= h.t->data);}int operator>=(const HufmNode& h) {return (t->data >= h.t->data);}
};
int mincost=0;
template<class T>
BTNode<T>* MakeHufm(const T*pa, int n)
{HufmNode<T>hf;BTNode<T>* t, * left, * right;Heap<HufmNode<T> >H(n);for (int i = 0; i < n; i++){t = GetNode(pa[i]);hf.t = t;H.Insert(hf);}for (int i = 1; i < n; i++){H.Deletemin(hf);left = hf.t;H.Deletemin(hf);right = hf.t;t = GetNode(left->data + right->data, left, right);mincost+=(left->data+right->data);hf.t = t;H.Insert(hf);}H.Deletemin(hf);t = hf.t;return t;
}int main(){int n;int sum=0;int x;BTNode<int>*t;int a[10005];cin>>n;for(int i=0;i<n;i++){cin>>x;a[i]=x;}t=MakeHufm(a,n);cout<<mincost<<endl;return 0;
}

PTA数据结构与算法题目集(中文)7-29相关推荐

  1. PTA数据结构与算法题目集6-4 6-3 6-8

    PTA数据结构与算法题目集(中文) 6-4 链式表的按序号查找 ElementType FindKth( List L, int K ){int index = 0;while(L){++index; ...

  2. PTA数据结构与算法题目集 6-9 二叉树的遍历

    PTA数据结构与算法题目集(中文) 6-9 二叉树的遍历 void InorderTraversal( BinTree BT ){if(BT==NULL)return;if(BT->Left){ ...

  3. PTA 数据结构与算法题目集(中文)

    一:数据结构与算法题目(中文版) 7-2 一元多项式的乘法与加法运算 (20 分) 7-3 树的同构 (25 分) 7-4 是否同一棵二叉搜索树 (25 分) 7-6 列出连通集 (25 分)(详解) ...

  4. PTA 数据结构与算法题目集(中文) 7-10 公路村村通 (30分) 最小生成树(kruskal算法)

    我的GIS/CS学习笔记:https://github.com/yunwei37/ZJU-CS-GIS-ClassNotes <一个浙江大学本科生的计算机.地理信息科学知识库 > 还有不少 ...

  5. 浙大PTA数据结构与算法题目集(中文)题解集复习用

    文章目录 7-1 最大子列和问题 (20分)(dp或贪心) 7-2 一元多项式的乘法与加法运算 (20分) 7-3 树的同构 (25分) 7-4 是否同一棵二叉搜索树 (25分) 7-5 堆中的路径 ...

  6. PTA数据结构与算法题目集(中文)7-25

    题意:找出M个俱乐部中最大朋友圈数量,朋友的朋友也朋友,所以可以采用并查集写此算法. 开始自己把题目理解错了,最后看了大神的写法终于明白了. #include <iostream> #in ...

  7. PTA数据结构与算法题目集(中文) 函数题 (1)

    4-1 单链表逆转 code: List Reverse(List head) {if(NULL==head|| NULL==head->Next)return head;List p;List ...

  8. PTA数据结构与算法题目集(中文)7-36

    Floyd算法求解: #include<stdio.h> #include<stdlib.h> #include<math.h> #define max 50005 ...

  9. PTA数据结构与算法题目集(中文)7-18

    题意:有两个窗口A,B,题目给出A窗口处理完两个顾客,B窗口处理完一个顾客,并且当不同窗口同时处理完2个顾客时,A窗口顾客优先输出.我们可以给出两个队列q1,q2分别表示A,B窗口,编号为奇数的顾客存 ...

  10. PTA数据结构与算法题目集(中文)7-45

    map的应用: #include <cstdio> #include <cstring> #include <algorithm> #include <ios ...

最新文章

  1. javascript计时器_JavaScript计时器:您需要了解的一切
  2. css小技巧 -- 单标签实现单行文字居中,多行文字居左
  3. nodejs windows 安装过程
  4. [导入]使用RDLC报表(一)
  5. 从0搭建一个Springboot+vue前后端分离项目(一)安装工具,创建项目
  6. cacti-0.8.8a那点儿事
  7. 电脑怎么进入linux系统,Linux操作系统进入家用电脑成为发展新前景
  8. ftp连接服务器连接不上,xshell可以连接上
  9. VS2015安装Visual C++的Win32控制台应用程序
  10. 罗马数字转换python_阿拉伯数字转换成罗马数字
  11. 大数据监测及预警系统平台怎么选择的方法参考
  12. 悲伤是一种毒,会上瘾
  13. vivo S16/S15/S12/S10 PRO卡刷线刷系统升级降级推荐成功解决屏幕锁不记得开机锁成功刷好的有效方案
  14. Linq to sql 求和操作
  15. graphics.h头文件图形绘画详解(史上最详细)
  16. 单元测试——Unittest(测试报告)
  17. MDSF:Mendix介绍
  18. R语言h2o深度学习分类
  19. oracle trim没用,Oracle中Trim函数的使用方法
  20. 网站被服务器禁,又一次腾讯云服务器网站被封禁 设备存在违规信息被限制访问 - 小俊学习网...

热门文章

  1. 数据结构之二分查找(折半查找)
  2. MySQL数据库子查询
  3. ACMNO.44 C语言-平均分 有N个学生,每个学生的数据包括学号、姓名、3门课的成绩,从键盘输入N个学生的数据,要求打印出3门课的总平均成绩,以及最高分的学生的数据(包括学号、姓名、3门课成绩)
  4. 基于OpenCV提取特定区域方法汇总
  5. 高校疯传!法国TOP双硕算法专家匠心打造一套保姆级AI学习笔记并公开(保姆级/20G高清/PPT/代码)...
  6. Java基础学习总结(22)——异常处理
  7. JPA(七):映射关联关系------映射双向多对一的关联关系
  8. 冰箱温度调到这个数值,就能节省超过35万吨食物
  9. 转:一个android开发者独立开发社交app全过程
  10. Mysql性能优化方案