1002: 哈夫曼树
Time Limit: 1 Sec Memory Limit: 65535 MB 64bit IO Format: %lldSubmitted: 13 Accepted: 6
[ Submit][ Status][ Web Board]
Description
根据给定的若干权值可以构造出一颗哈夫曼树。构造的哈夫曼树可能不唯一,但是按照下面的选取原则所构造出来的哈夫曼树应该是唯一的。
(1)每次选取优先级最低的两个结点,优先级最低的作为左子树,优先级高的作为右子树;
(2)结点优先级大小的比较首先比较它们的权值,权值大的优先级高,权值小的优先级低,权值相等的按照位置关系来比较,位置在前面的优先级低,位置在后面的优先级高。
(3)增加的新结点位置依次往后。
根据你所构造的哈夫曼树来设计每个权值的哈夫曼编码(左子树0右子树1),并计算该哈夫曼树的WPL值。
Input
包含多组数据
每组数据包含2行,第一行输入权值个数n(0<n<20),第2行为顺序输入的n个权值,均为整数(小于100),
Output
对于每组数据,输出n+1行,前面n行为每个权值所对应的赫夫曼编码(按照输入顺序给出),第n+1行用来输出你所构造的哈夫曼树的WPL值。
Sample Input
5
11 4 2 5 7
6
2 3 4 7 8 9
Sample Output
11
011
010
00
10
64
1110
1111
110
00
01
10
80
#include <bits/stdc++.h>
#include <iostream>
#include <stdio.h>
#include <string>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
struct Node
{
int a,cnt,le,ri;
}f[4009];
string ans[4009];
int wpl;
int n;
priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > >pq;
void dfs(int x,int num,string ss)
{
if(f[x].le!=-1)
{
dfs(f[x].le,num+1,ss+'0');
}
if(f[x].ri!=-1)
{
dfs(f[x].ri,num+1,ss+'1');
}
if(x<n)
{
ans[x]=ss;
wpl+=f[x].a*num;
}
}
int main()
{
while(~scanf("%d",&n))
{
int cnt=0;
while(!pq.empty())pq.pop();
int a;
for(cnt=0;cnt<n;cnt++)
{
scanf("%d",&a);
pq.push(make_pair(a,cnt));
f[cnt].a=a;
f[cnt].cnt=cnt;
f[cnt].le=-1;f[cnt].ri=-1;
}
while(pq.size()>1)
{
pair<int,int>le=pq.top();
pq.pop();
pair<int,int>ri=pq.top();
pq.pop();
int fa=le.first+ri.first;
pq.push(make_pair(fa,cnt));
f[cnt].a=fa;
f[cnt].cnt=cnt;
f[cnt].le=le.second;
f[cnt].ri=ri.second;
cnt++;
}
int root=pq.top().second;
wpl=0;
dfs(root,0,"");
for(int i=0;i<n;i++)
cout<<ans[i]<<endl;
printf("%d\n",wpl);
}
return 0;
}