当前位置: 首页 > wzjs >正文

西安市建设干部学校网站珠海网站建设优化

西安市建设干部学校网站,珠海网站建设优化,wordpress 字体不好看,ps网站首页设计题目描述 哈夫曼树,第一行输入一个数n,表示叶结点的个数。需要用这些叶结点生成哈夫曼树,根据哈夫曼树的概念,这些结点有权值,即weight,题目需要输出所有结点的值与权值的乘积之和。 输入 输入有多组数据…
题目描述

哈夫曼树,第一行输入一个数n,表示叶结点的个数。需要用这些叶结点生成哈夫曼树,根据哈夫曼树的概念,这些结点有权值,即weight,题目需要输出所有结点的值与权值的乘积之和。

输入

输入有多组数据。
每组第一行输入一个数n,接着输入n个叶节点(叶节点权值不超过100,2<=n<=1000)。

输出

输出权值。

样例输入
2
2 8 
3
5 11 30 
样例输出
10
62

分析:先建立哈夫曼树,然后求出每个节点的深度,再与权值相乘,最后求出所有节点的带权路径长度的之和输出。代码可以参考问题B。

#include<algorithm>
#include <iostream>
#include  <cstdlib>
#include  <cstring>
#include   <string>
#include   <vector>
#include   <cstdio>
#include    <queue>
#include    <stack>
#include    <ctime>
#include    <cmath>
#include      <map>
#include      <set>
#define INF 0xffffffff
#define db1(x) cout<<#x<<"="<<(x)<<endl
#define db2(x,y) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<endl
#define db3(x,y,z) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<", "<<#z<<"="<<(z)<<endl
#define db4(x,y,z,r) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<", "<<#z<<"="<<(z)<<", "<<#r<<"="<<(r)<<endl
#define db5(x,y,z,r,w) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<", "<<#z<<"="<<(z)<<", "<<#r<<"="<<(r)<<", "<<#w<<"="<<(w)<<endl
using namespace std;typedef struct node
{int val,index;struct node *lchild,*rchild,*father;
}node;struct CompareNodePtr
{bool operator()(const node *a, const node *b)const{if(a->val==b->val)return a->index>b->index;  // val 相同,则 index 小的优先return a->val>b->val;  // val 小的优先}
};void dfs(int ss[],node *root,int temp,int n)
{if(root==NULL)return;
//    db2(root->val,root->index);if(root->index<=n&&root->index>=1){
//        db1(root->val);ss[root->index]=temp*root->val;return;}dfs(ss,root->lchild,temp+1,n);dfs(ss,root->rchild,temp+1,n);
}void Free(node *root)
{if(root==NULL)return;Free(root->lchild);Free(root->rchild);free(root);return;
}int main(void)
{#ifdef testfreopen("in.txt","r",stdin);
//    freopen("out.txt","w",stdout);clock_t start=clock();#endif //testint n;while(~scanf("%d",&n)){vector<node*>num(n+5);priority_queue<node*,vector<node*>,CompareNodePtr>q;for(int i=0;i<n;++i){num[i]=(node*)malloc(sizeof(node));num[i]->lchild=num[i]->rchild=num[i]->father=NULL;num[i]->index=i+1;  // 赋值索引scanf("%d",&num[i]->val);q.push(num[i]);}int mergeIndex=n+1;node *x,*y;while(q.size()>1){x=q.top(),q.pop();y=q.top(),q.pop();// 确保 x->index < y->index 以符合 `seletMin` 逻辑if(x->index>y->index)swap(x,y);node *temp=(node*)malloc(sizeof(node));temp->val=x->val+y->val;temp->lchild=x,temp->rchild=y;x->father=y->father=temp;temp->index=mergeIndex++;  // 赋予新的索引值q.push(temp);}node *root=q.top();q.pop();int ss[n+5]={0};dfs(ss,root,0,n);int ans=0;for(int i=1;i<=n;++i)ans+=ss[i];printf("%d\n",ans);Free(root);}#ifdef testclockid_t end=clock();double endtime=(double)(end-start)/CLOCKS_PER_SEC;printf("\n\n\n\n\n");cout<<"Total time:"<<endtime<<"s"<<endl;        //s为单位cout<<"Total time:"<<endtime*1000<<"ms"<<endl;    //ms为单位#endif //testreturn 0;
}

http://www.dtcms.com/wzjs/379611.html

相关文章:

  • 网站开发文章营销和销售的区别在哪里
  • 自己电脑做局域网网站服务器seo综合查询国产
  • 怎么给网站做备案常用的网络营销工具有哪些
  • 个人淘客网站备案故事性营销软文
  • 购物网站建设报价做竞价推广这个工作怎么样
  • 深圳住房建设局网站申报百度识图查图片
  • 毕设做桌面端还是网站刷神马网站优化排名
  • 南宁好的网站建设公司怎么做自己的网站
  • 管理型网站建设费用明细河南网站推广那家好
  • 建立一个网站的流程cba最新积分榜
  • 单页网站 挣钱安全优化大师
  • 易优cms收费吗专业seo优化公司
  • 抚州做网站百度关键词优化大师
  • 微小旅行社能否做网站云速seo百度点击
  • 武汉哪些网站建设百度收录查询api
  • 网站设计与制作优点河北高端网站建设
  • 多语言多风格网站方案省好多会员app
  • 百度云服务器做php网站南平seo
  • 求做图的网站百度上做推广怎么收费
  • 铁建设文件在什么网站下载搜索引擎营销是什么意思
  • 品牌展示榜ui做的好的网站网页优化seo公司
  • 优惠券网站怎么做的淘宝的前100个关键词排名
  • wordpress 更改icon网站快速优化排名官网
  • 做网站网页免费推广引流平台
  • 哪个平台做网站好天天外链官网
  • 广州有哪些做网站专业的公司搜索引擎优化方法总结
  • 做网站软件图标是一个箭头的搜狗推广登录
  • 二手交易网站设计怎么做seo数据分析哪些方面
  • 为什么做网站必须用服务器网络推广培训班
  • 网站开发是web开发吗b站视频怎么快速推广