哈夫曼树的简单编程-Minimum Weight


Description

Given a list of weights w1, w2, ..., wn, we can construct a binary tree with n leaves, where each leaf is marked with one weight. The is called a weighted binary tree. The weight of the tree is w1l1 + w2l2+ ... wnln,  where li is  the height of the leaf with weight wi 

For a given list of weights, there are many weighted trees with these weights. However, different weighted tree will have different weights. You are asked to write a program to find the mimimum weight among those weighted trees for a given list of weights.

 

Input

The first line is the number  n of test cases.

The next n lines are the test cases. Each test case is a list of weights that are positive numbers separated by spaces  followed by the number 0, which signals the end of the weights.  

Output

Each line outputs the minimum weight for the test case.

很明显 这是一道构造一棵哈夫曼树。具体的构造如下所示。参考代码。


#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
#include <set>
using namespace std;

struct huffman{
    int sum;
    int leftc;
    int rightc;
    int index;
    int high;
    friend bool operator<(huffman n1, huffman n2)
    {
        return n1.sum < n2.sum;
    }
};

void mycout(huffman temp[], int index, int height)
{
    if (temp[index].leftc == -1 && temp[index].rightc == -1)
    {
        temp[index].high = height;
        return ;
    }
    else
    {
        mycout(temp, temp[index].leftc, height + 1);
        mycout(temp, temp[index].rightc, height + 1);
    }
    return ;
}

int main()
{
    const int max = 9999009;
    int n;
    cin >> n;
    int num;
    while (n --)
    {
        huffman node[1009];
        int j = 0;
        multiset<huffman> myset;
        while (1)
        {
            cin >> num;
            if (num == 0) break;
            node[j].sum = num;
            node[j].leftc = -1;
            node[j].rightc = -1;
            node[j].index = j;
            myset.insert(node[j]);
            ++ j;           
        }
        int allcot = j;
        while (myset.size() > 1)
        {
            huffman temp1, temp2;
            multiset<huffman>::iterator it = myset.begin();
            temp1 = (*it);
            myset.erase(it);
            it = myset.begin();
            temp2 = *it;
            myset.erase(it);
            node[j].sum = temp1.sum + temp2.sum;
            node[j].leftc = temp1.index;
            node[j].rightc = temp2.index;
            node[j].index = j;
            myset.insert(node[j]);
            ++ j;
        }
        mycout(node, j - 1, 0);
        int number = 0;
        for (int i = 0; i < allcot; ++ i)
        {
            //cout << node[i].sum << " " << node[i].high << endl;
            number += node[i].sum * node[i].high;
        }
        cout << number << endl;
    }
}                                 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值