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 2014CCUTlch at 2016-01-12 11:28:16 on Problem 3253
#include<iostream>
using namespace std;
int i,j,k;
const int MaxSize=20001;
struct element
{
	int weight;
	int lchild,rchild,parent;
};

void Select(element huffTree[],int n,int m) 
{
	int tmp;
	long long sum=0;  
	for(i=n+1;i<=m;i++) 
	{
		int min1,min2,s1,s2; 
		min1=min2=MaxSize;  
		for(j=1;j<=i-1;j++) 
		{ 
			if(huffTree[j].parent==-1&&huffTree[j].weight<min1) 
			{ 
				min1=huffTree[j].weight;
				s1=j;
			} 
		}  
		for(k=1;k<=i-1;k++)  
		{ 
			if(huffTree[k].parent==-1&&k!=s1&&huffTree[k].weight<min2)
			{ 
				min2=huffTree[k].weight;
				s2=k;
			} 
		}   
		if(min1>=min2)  
		{
			tmp=s1;
			 s1=s2; 
			 s2=tmp;
		} 
		huffTree[s1].parent=i;
		huffTree[s2].parent=i;
		huffTree[i].lchild=s1;
		huffTree[i].rchild=s2;
		huffTree[i].weight=huffTree[s1].weight+huffTree[s2].weight;
		sum+=huffTree[i].weight;	
 	}
	 cout<<sum<<endl;
}

void HuffmanTree(element huffTree[],int w[],int n)
{
	for(i=1;i<=2*n-1;i++)
	{
		huffTree[i].parent=-1;
		huffTree[i].lchild=-1;
		huffTree[i].rchild=-1;
	}
	for(i=1;i<=n;i++)
		huffTree[i].weight=w[i];
	for(k=n;k<2*n-1;k++)
		Select(huffTree,k,2*n-1);
}
int main()
{
	element huff[MaxSize];
	int a[MaxSize],n;
	cin>>n;
	for(i=1;i<=n;i++)
		cin>>a[i];
	HuffmanTree(huff,a,n);
	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