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 |
我的也是用哈夫曼WA了,In Reply To:构造哈夫曼树,不知道为什么事WA?能帮看看不? Posted by:LarryYe at 2009-11-13 10:47:32 //之前用简化了的哈夫曼树过了,想写个完整的哈夫曼但老是wa,高手帮忙找找哪错了? #include<iostream> using namespace std; #define MAX 20002 #define Vmax 60000 struct Node { double data; bool visit; int parent; int leftson; int rightson; }; Node p[2*MAX]; double value[MAX]; int n; // void hafuman()//哈夫曼树 { int i,j,x1,x2; double m1,m2; for(i=0;i<2*n-1;i++)//树的初始化 { if(i<n) p[i].data=value[i]; else p[i].data=0; p[i].visit=false; p[i].parent=-1; p[i].leftson=-1; p[i].rightson=-1; } for(i=0;i<n-1;i++)//建树 { m1=Vmax; m2=Vmax; x1=0; x2=0; for(j=0;j<n+i;j++)//查找最小的两个数所对应的下标x1,x2 { if(p[j].data<m1&&!p[j].visit) { m2=m1; x2=x1; m1=p[j].data; x1=j; } else if(p[j].data<m2&&!p[j].visit) { m2=p[j].data; x2=j; } } //加一点,去最小两数点 p[x1].parent=n+i; p[x2].parent=n+i; p[x1].visit=true; p[x2].visit=true; p[n+i].data=p[x1].data+p[x2].data; p[n+i].leftson=x1; p[n+i].rightson=x2; } } // int main() { // freopen("in.txt","r",stdin); int i; double sum; scanf("%d",&n); for(i=0;i<n;i++) scanf("%lf",&value[i]);//权 hafuman(); sum=0; for(i=n;i<2*n-1;i++)//非枝点数相加为所求 sum+=p[i].data; printf("%.0lf\n",sum); return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator