九度OJ1172:哈夫曼树
2016-03-12 16:25
375 查看
[b]来源:[/b]
[b]http://ac.jobdu.com/problem.php?pid=1172[/b]
题目描述:
哈夫曼树,第一行输入一个数n,表示叶结点的个数。需要用这些叶结点生成哈夫曼树,根据哈夫曼树的概念,这些结点有权值,即weight,题目需要输出所有结点的值与权值的乘积之和。
输入:
输入有多组数据。
每组第一行输入一个数n,接着输入n个叶节点(叶节点权值不超过100,2<=n<=1000)。
输出:
输出权值。
样例输入:
样例输出:
[b]http://ac.jobdu.com/problem.php?pid=1172[/b]
题目描述:
哈夫曼树,第一行输入一个数n,表示叶结点的个数。需要用这些叶结点生成哈夫曼树,根据哈夫曼树的概念,这些结点有权值,即weight,题目需要输出所有结点的值与权值的乘积之和。
输入:
输入有多组数据。
每组第一行输入一个数n,接着输入n个叶节点(叶节点权值不超过100,2<=n<=1000)。
输出:
输出权值。
样例输入:
5 1 2 2 5 9
样例输出:
37
方法一:构建Haffman Tree, 然后计算叶节点的权值。
<pre name="code" class="cpp">#include <stdio.h> #include <algorithm> using namespace std; struct Node { int e; Node *lchild, *rchild; }; bool cmp(Node *a, Node *b) { return a->e < b->e; } Node *arr[100]; int CreateHaffmanTree(int n) { int i=0; while(i<n-1) { Node *root = (Node*)malloc(sizeof(Node)); root->lchild = arr[i]; root->rchild = arr[i+1]; root->e = arr[i]->e + arr[i+1]->e; arr[++i] = root; sort(arr+i,arr+n,cmp); } return 0; } int countHaffmanTree(Node *root, int level) { if(root->lchild || root->rchild) { return countHaffmanTree(root->lchild, level+1)+ countHaffmanTree(root->rchild, level+1); } else return root->e * level; } int main() { freopen("in.txt", "r", stdin); int n,e; scanf("%d",&n); for(int i=0; i<n; i++) { scanf("%d",&e); Node *root = (Node*)malloc(sizeof(Node)); root->e = e; root->lchild = NULL; root->rchild = NULL; arr[i] = root; } sort(arr, arr+n, cmp); CreateHaffmanTree(n); printf("%d\n",countHaffmanTree(arr[n-1], 0)); return 0; }方法二:不建树,每次吧排序后最小的两个值加入权值和,将还未统计过的值和新产生的值(最小的两个数的和)重新排序。
<pre name="code" class="cpp"> 7e82 #include <stdio.h> #include <algorithm> using namespace std; int main() { freopen("in.txt","r",stdin); int n, sum = 0; int arr[100]; scanf("%d",&n); for(int i=0; i<n; i++) { scanf("%d",&arr[i]); } sort(arr,arr+n); i = 0; while(i<n-1) { sum+= arr[i]+arr[i+1]; arr[i+1] = arr[i]+arr[i+1]; i++; sort(arr+i,arr+n); } for(i=0; i<n; i++) printf("%d ",arr[i]); printf("%d ",sum); return 0; }
相关文章推荐
- Static 变量引发的NullPointerException
- Physics.Raycast和Ray(射线)的理解
- 增加bluestacks的存储空间- 4G
- 20135323符运锦----第三周:构建一个简单的Linux系统MenuOS
- 关于软件工程文档解析
- 软件工程第二周学习进度表
- hdu Dragon Balls 3635【并查集】
- HDU 1203 I NEED A OFFER!
- 实时SLAM的未来及与深度学习的比较The Future of Real-Time SLAM and “Deep Learning vs SLAM”
- Android 混淆代码总结
- react.js-10-渐隐动画
- MySQL - Entity : The value for column 'IsPrimaryKey' in table 'TableDetails' is DBNull
- 流式大数据处理的三种框架:Storm,Spark和Samza
- HDFS源码分析DataXceiver之读数据块
- Hibernate 一级缓存和二级缓存使用
- Java Web开发中路径问题小结
- CSS——字体缩写
- 数组
- 两个链表的第一个公共节点
- 计算机视觉Computer Vision领域博客资源