zoukankan      html  css  js  c++  java
  • 优先队列学习之哈弗曼树

    转载自:http://www.cppblog.com/darren/archive/2009/06/09/87224.html

    priority_queue 调用 STL里面的 make_heap(), pop_heap(), push_heap() 算法
    实现,也算是堆的另外一种形式。

    先写一个用 STL 里面堆算法实现的与真正的STL里面的 priority_queue  用法相
    似的 priority_queue, 以加深对 priority_queue 的理解

    #include <iostream>
    #include <algorithm>
    #include <vector>
    
    using namespace std;
    
    class priority_queue
    {
        private:
            vector<int> data;
            
        public:
            void push( int t ){ 
                data.push_back(t); 
                push_heap( data.begin(), data.end()); 
            }
            
            void pop(){
                pop_heap( data.begin(), data.end() );
                data.pop_back();
            }
            
            int top()    { return data.front(); }
            int size()   { return data.size();  }
            bool empty() { return data.empty(); }
    };
    
    
    int main()
    {
        priority_queue  test;
        test.push( 3 );
        test.push( 5 );
        test.push( 2 );
        test.push( 4 );
        
        while( !test.empty() ){
            cout << test.top() << endl;
            test.pop(); }
            
        return 0;
    }

    STL里面的 priority_queue 写法与此相似,只是增加了模板及相关的迭代器什么的。


    priority_queue 对于基本类型的使用方法相对简单。
    他的模板声明带有三个参数,priority_queue<Type, Container, Functional>
    Type 为数据类型, Container 为保存数据的容器,Functional 为元素比较方式。
    Container 必须是用数组实现的容器,比如 vector, deque 但不能用 list.
    STL里面默认用的是 vector. 比较方式默认用 operator< , 所以如果你把后面俩个
    参数缺省的话,优先队列就是大顶堆,队头元素最大。
    看例子

    #include <iostream>
    #include <queue>
    
    using namespace std;
    
    int main(){
        priority_queue<int> q;
        
        for( int i= 0; i< 10; ++i ) q.push( rand() );
        while( !q.empty() ){
            cout << q.top() << endl;
            q.pop();
        }
        
        getchar();
        return 0;
    }


    如果要用到小顶堆,则一般要把模板的三个参数都带进去。
    STL里面定义了一个仿函数 greater<>,对于基本类型可以用这个仿函数声明小顶堆
    例子:

    #include <iostream>
    #include <queue>
    
    using namespace std;
    
    int main(){
        priority_queue<int, vector<int>, greater<int> > q;
        
        for( int i= 0; i< 10; ++i ) q.push( rand() );
        while( !q.empty() ){
            cout << q.top() << endl;
            q.pop();
        }
        
        getchar();
        return 0;
    }


     对于自定义类型,则必须自己重载 operator< 或者自己写仿函数
    先看看例子:

    #include <iostream>
    #include <queue>
    
    using namespace std;
    
    struct Node{
        int x, y;
        Node( int a= 0, int b= 0 ):
            x(a), y(b) {}
    };
    
    bool operator<( Node a, Node b ){
        if( a.x== b.x ) return a.y> b.y;
        return a.x> b.x; 
    }
    
    int main(){
        priority_queue<Node> q;
        
        for( int i= 0; i< 10; ++i )
        q.push( Node( rand(), rand() ) );
        
        while( !q.empty() ){
            cout << q.top().x << ' ' << q.top().y << endl;
            q.pop();
        }
        
        getchar();
        return 0;
    }


    自定义类型重载 operator< 后,声明对象时就可以只带一个模板参数。
    但此时不能像基本类型这样声明
    priority_queue<Node, vector<Node>, greater<Node> >;
    原因是 greater<Node> 没有定义,如果想用这种方法定义
    则可以按如下方式

    例子:

    #include <iostream>
    #include <queue>
    
    using namespace std;
    
    struct Node{
        int x, y;
        Node( int a= 0, int b= 0 ):
            x(a), y(b) {}
    };
    
    struct cmp{
        bool operator() ( Node a, Node b ){
            if( a.x== b.x ) return a.y> b.y;
            
            return a.x> b.x; }
    };
    
    int main(){
        priority_queue<Node, vector<Node>, cmp> q;
        
        for( int i= 0; i< 10; ++i )
        q.push( Node( rand(), rand() ) );
        
        while( !q.empty() ){
            cout << q.top().x << ' ' << q.top().y << endl;
            q.pop();
        }
        
        getchar();
        return 0;
    }

    --------------------------------------

    如下转载自:http://blog.csdn.net/zhc_ac/article/details/47395399

    题目描述

     在一个果园里,多多已经将所有的果子打了下来,而且按果子的不同种类分成了不同的堆。多多决定把所有的果子合成一堆。
    每一次合并,多多可以把两堆果子合并到一起,消耗的体力等于两堆果子的重量之和。可以看出,所有的果子经过n-1次合并之后,就只剩下一堆了。多多在合并果子时总共消耗的体力等于每次合并所消耗体力之和。
    因为还要花大力气把这些果子搬回家,所以多多在合并果子时要尽可能地节省体力。假定每个果子重量都为1,并且已知果子的种类数和每种果子的数目,你的任务是设计出合并的次序方案,使多多耗费的体力最少,并输出这个最小的体力耗费值。
    例如有3种果子,数目依次为1,2,9。可以先将1、2堆合并,新堆数目为3,耗费体力为3。接着,将新堆与原先的第三堆合并,又得到新的堆,数目为12,耗费体力为12。所以多多总共耗费体力=3+12=15。可以证明15为最小的体力耗费值。
     

    输入

     第一行是一个整数n(1<=n<=10000),表示果子的种类数。第二行包含n个整数,用空格分隔,第i个ai(1<=ai<=20000)是第i个果子的数目。
     

    输出

     输出包括一行,这一行只包含一个整数,也就是最小的体力耗费值。输入数据保证这个值小于2^31。
     

    示例输入

    3
    1 2 9

    示例输出

    15
    #include <iostream>
    #include <stdio.h>
    #include <string.h>
    #include <queue>//头文件
    
    using namespace std;
    
    struct cmp//函数从小到大排序,因为默认的优先队列是从大到小排序
    {
        bool operator ()(long long &a,long long &b)
        {
            return a>b;
        }
    };
    
    int main()
    {
        priority_queue<long long ,vector<long long>,cmp>q;//优先队列定义方式
    
        long long  a;
        int n,i;
    
        while(scanf("%d",&n)!=EOF)
        {
            long long  x=0,y=0;
            long long  ans = 0,sum = 0;
    
            for(i=0;i<n;i++)
            {
                scanf("%lld",&a);
                q.push(a);//把a加入到优先队列q中
            }
            while(!q.empty())//当队列里还有数存在时
            {
                x = q.top();//从队列中取出
                q.pop();//把队列中的第一个字符删除
                if(!q.empty())
                {
                       y = q.top();
                       q.pop();
                        ans = x+y;
                        sum = sum + ans;
                        q.push(ans);
                }
            }
            printf("%lld
    ",sum);
        }
        return 0;
    }
  • 相关阅读:
    Flash实现图片上传(实现浏览器端图片预览)
    Visual Studio自定义模板参数说明
    JS & jQuery上下无缝滚动应用(单行或多行)
    批处理bat文件得到当前绝对路径、盘符等
    JavaScript判断IE的最短写法
    [转载]替代Apache和IIS的轻量级网络服务器盘点
    unable to start debugging on the web server. An authentication error occurred while communicating with the web server.
    开放的云平台
    [转载]15个最优秀的免费开源电子商务交易平台
    MS Sql Server导出表结构
  • 原文地址:https://www.cnblogs.com/hellowooorld/p/7282516.html
Copyright © 2011-2022 走看看