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

顺德做网站那家好微商店铺怎么开通

顺德做网站那家好,微商店铺怎么开通,公司宣传片的拍摄,seo工具包括题目描述 哈夫曼树,第一行输入一个数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/258935.html

相关文章:

  • 微信网站这么做成品短视频软件大全下载手机版
  • app网站区别推广运营是做什么的
  • 广东的网站建设百度爱采购服务商查询
  • 北京中燕建设公司网站站长工具综合查询官网
  • 河海大学土木专业类建设网站什么是百度快照
  • 西安到北京飞机湖南seo服务电话
  • 河南城乡建设网站怎么自己做个网站
  • b2b网站推广优化惠州seo计费
  • 做非洲国际贸易网站互联网营销师教材
  • 网站与经营网站下拉框关键词软件
  • wordpress如何实现301跳转优化设计三年级上册答案语文
  • 摄影网站建设的目的网站模板下载免费
  • 最新的网站建设软件有哪些手机助手
  • 一级a做爰片免费网站孕交视频教程合肥关键词排名优化
  • wordpress用户中心按钮不弹出企业seo网站营销推广
  • 宣威网站建设公司济南网站建设公司
  • 做网站做推广有效果吗现在最好的免费的建站平台
  • wordpress 制作手机站百度竞价排名推广
  • 仿站多少钱一套发布平台
  • 工业设计网站哪家好5118
  • 网站开发的方案网络营销公司注册找哪家
  • 商旅平台app官网下载厦门seo外包
  • 苏州网站推广建设百度seo2022
  • 宾馆网站制作北京全网营销推广
  • 广东快速做网站公司哪家好长沙网站关键词排名公司
  • 江西做网站找谁湘潭网站设计外包服务
  • 南京哪里做网站销售网络平台推广
  • 网站建设质量要求快速排名点击工具
  • 南宁网站建设怎么样谷歌推广网站
  • 企业网站推广推广阶段自己如何制作一个网站