哈夫曼樹

1002: 哈夫曼樹

Time Limit: 1 Sec  Memory Limit: 65535 MB   64bit IO Format: %lld
Submitted: 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;
}











發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章