http://poj.org/problem?id=3253
这道题约等于合并果子,但是通过这道题能够看出来哈夫曼树是什么了。
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<cmath> 5 #include<iostream> 6 #include<queue> 7 using namespace std; 8 #define LL long long 9 const int maxn=300010; 10 int n; 11 priority_queue< LL,vector<LL>,greater< LL > >q; 12 int main(){ 13 while(~scanf("%d",&n)){ 14 LL x,y; 15 for(int i=1;i<=n;i++){scanf("%lld",&x);q.push(x);} 16 LL ans=0; 17 while(!q.empty()){ 18 x=q.top();q.pop(); 19 if(q.empty())continue; 20 y=q.top();q.pop(); 21 ans+=x+y; 22 q.push(x+y); 23 } 24 printf("%lld\n",ans); 25 while(!q.empty())q.pop(); 26 } 27 return 0; 28 }