zoukankan      html  css  js  c++  java
  • 优先队列和哈夫曼树

      https://www.acwing.com/problem/content/description/150/

    哈夫曼树可以通过小根堆实现。小根堆每次弹出两个值,然后将二者的和再插入小根堆中。

      比如求

    #include<stdio.h>
    #include<queue>
    using namespace std;
    int n,ans,k;
    priority_queue<int,vector<int>,greater<int> > q;//小根堆
    int main(){
        scanf("%d",&n);
        for(int i=1;i<=n;i++){
            scanf("%d",&k);
            q.push(k);
        }
        for(int i=1;i<n;i++){
            int a=q.top();
            q.pop();
            int b=q.top();
            q.pop();
            ans+=a+b;
            q.push(a+b);
        }
        printf("%d",ans);
        return 0;
    }
    

      

    加油啦!加油鸭,冲鸭!!!
  • 相关阅读:
    算法70----只有两个键的键盘【动态规划】
    Shell
    Shell
    Shell
    Shell
    Shell
    Tools
    Jenkins
    Java
    Product
  • 原文地址:https://www.cnblogs.com/clarencezzh/p/12072130.html
Copyright © 2011-2022 走看看