Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

Re:C写的,测试没错误,求大神找找哪里错了,(哈夫曼树写的)贴代码

Posted by sfsdufe202018140210 at 2022-04-19 17:54:08 on Problem 3253
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:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator