| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
Re:C写的,测试没错误,求大神找找哪里错了,(哈夫曼树写的)贴代码In Reply To:C写的,测试没错误,求大神找找哪里错了,(哈夫曼树写的)贴代码 Posted by:nobleman at 2017-01-22 13:54:02 > #include<stdio.h>
> #include<stdlib.h>
> typedef long long ll;
> typedef struct{
> int weight;
> int parent,lchild,rchild;
>
> }HTNode,*HuffmanTree;
> void main()
> {
> HuffmanTree HT;
> int n,i,m,j,s1,s2;
> ll sum=0;
> scanf("%d",&n);
> if(n<=1)
> return;
> m=2*n-1;
> HT=(HuffmanTree)malloc((m+1)*sizeof(HTNode));
> for(i=1;i<=n;i++)
> scanf("%d",&HT[i].weight);
> for(i=1;i<=m;i++)
> {
> HT[i].parent=0;
> HT[i].lchild=0;
> HT[i].rchild=0;
> }
> for(i=n+1;i<=m;i++)
> {
> s1=s2=0;
> for(j=1;j<=m;j++)
> {
> if(HT[j].parent==0)
> {
> if(HT[j].weight<HT[s1].weight)
> s1=j;
> }
> }
> for(j=1;j<=m;j++)
> {
> if(HT[j].parent==0&&j!=s1)
> {
> if(HT[j].weight<HT[s2].weight)
> s2=j;
> }
> }
> HT[s1].parent=i;
> HT[s2].parent=i;
> HT[i].lchild=s1;
> HT[i].rchild=s2;
> HT[i].weight=HT[s1].weight+HT[s2].weight;
> sum=sum+HT[i].weight;
> }
> printf("%lld\n",sum);
> }
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator