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

我的也是用哈夫曼WA了,

Posted by taishi at 2010-03-17 13:30:41 on Problem 3253
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:
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