首页 > 代码库 > 霍夫曼树计算 WPL

霍夫曼树计算 WPL

 1 #include<stdio.h> 2 #include<iostream> 3 #include<string> 4 #include<string.h> 5 #include<algorithm> 6 #include<iomanip> 7 #include<vector> 8 #include<time.h> 9 #include<queue>10 #include<stack>11 #include<iterator>12 #include<math.h>13 #include<stdlib.h>14 #include<limits.h>15 //#define ONLINE_JUDGE16 #define ll long long17 18 using namespace std;19 priority_queue <ll, vector<ll>, greater<ll> > Q;20 #define N 50005021 22 int main()23 {24     int n;25     while(scanf("%d",&n)!=EOF){26         for(int i=0;i<n;i++){27             int t;28             scanf("%d",&t);29             Q.push(t);30         }31         if(Q.size() > 1){32             ll num = 0;33             while(Q.size()>1){34                 ll n = Q.top();Q.pop();35                 ll m = Q.top();Q.pop();36                 num += (n+m);37                 Q.push(n+m);38             }39             printf("%lld\n",num);40         }else{41             int t = Q.top();42             printf("%d\n",t);43         }44     }45     return 0;46 }

 

霍夫曼树计算 WPL