九度OJ-1172-哈夫曼树

来源:互联网 发布:百度网盘会员破解mac 编辑:程序博客网 时间:2024/06/06 05:13

题目描述:
哈夫曼树,第一行输入一个数n,表示叶结点的个数。需要用这些叶结点生成哈夫曼树,根据哈夫曼树的概念,这些结点有权值,即weight,题目需要输出所有结点的值与权值的乘积之和。
输入:
输入有多组数据。
每组第一行输入一个数n,接着输入n个叶节点(叶节点权值不超过100,2<=n<=1000)。
输出:
输出权值。
样例输入:
5
1 2 2 5 9
样例输出:
37

#include <stdio.h>#include <stdlib.h>#include <math.h>int cmp(const void * a,const void *b){    return *(int *)b-*(int *)a;}/*模仿word2vec里创建Huffman树的方法*/int main(){    freopen("input.txt","r",stdin);    int n;    int * array;    int pos1,pos2,a;    int min1_index,min2_index;    while(scanf("%d",&n)!=EOF){        //printf("%d \n",n );        array =(int *) malloc((2*n-1)*sizeof(int));        for (int i = 0; i < n; ++i)            scanf("%d",array+i);        qsort(array,n,sizeof(int),cmp);        for (int i = n; i < 2*n-1; ++i)            array[i]=1000;        //for (int i = 0; i < 2*n-1; ++i)   printf("%d ",array[i]);        pos1 = n-1;        pos2 = n;        for (a = 0; a < n-1; ++a)        {            if(pos1>=0){                if (array[pos1]<array[pos2])                {                    min1_index=pos1;                    pos1--;                }else{                    min1_index=pos2;                    pos2++;                }            }else{                min1_index=pos2;                pos2++;            }            if(pos1>=0){                if (array[pos1]<array[pos2])                {                    min2_index=pos1;                    pos1--;                }else{                    min2_index=pos2;                    pos2++;                }            }else{                min2_index=pos2;                pos2++;            }            array[n+a]=array[min1_index]+array[min2_index];                 }        int sum=0;        for (int i = n; i < 2*n-1; ++i)            sum+=array[i];        printf("%d\n", sum);    }}
0 0