• 优先队列实现哈夫曼树


    //优先队列实现哈夫曼树
    #include<iostream>
    #include<cstdio>
    #include<queue>
    using namespace std;
    //法一
    struct cmp  //最小值优先
    {
     bool operator ()( int &a, int &b)
     {
      if(a!=b)
          return a>b;
     }
    };
    struct cmp  //最小值优先
    {
     bool operator ()(const int &a,const int &b)
     {
      if(a!=b)
          return a>b;
     }
    };
    struct cmp  //最大值优先
    {
     bool operator ()(const int &a,const int &b)
     {
      if(a!=b)
          return a<b;
     }
    };
    //法二
    //由大到小
    struct node
    {
     int x;
     friend bool operator <(const node &a,const node &b)
     {
      if(a.x!=b.x)
          return a.x<b.x;
     }
     } ;
     //法三
    struct node
    {
     int x;
     bool operator <(const node &a)const
     {
      
          return x<a.x;
     }
     } ;
    int main(void)
    {
     int n;
     int a;
     cin>>n;
     priority_queue<int,vector<int>,cmp> pq;//法一
     //priority_queue<node> pq;//法二 三
     for(int i=0;i<n;i++)
     {
      cin>>a;
      pq.push(a);
     }
     int val=0;
     while(pq.size()>1)
     {
      int min1=pq.top();
      pq.pop();
      int min2=pq.top();
      pq.pop();
      min1+=min2;
      val+=min1;
      pq.push(min1);
     }
     cout<<val<<endl;
     return 0;
     }

  • 相关阅读:
    Eclipse插件安装方法大全
    Weblogic web应用中获取文件的绝对路径
    Weblogic 设置优先引用web项目的jar包
    Oracle 数据库创建(图形界面操作)
    Java 输入输出流总结
    如何获取项目中文件的路径
    EclEmma安装与使用
    单向链表

    散列表
  • 原文地址:https://www.cnblogs.com/cxwpluto/p/12441141.html
Copyright © 2020-2023  润新知