Blue Bridge Cup basic training --Huffuman tree

Topic Link

 


Report problem solving:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 struct cmp {
 4     bool operator()(int a,int b){
 5         return a>b;
 6     }
 7 };
 8 int a[110];
 9 int main()
10 {
11     int n,v;
12     priority_queue<int,vector<int>,cmp>que;
13     cin>>n;
14     for(int i=1;i<=n;i++) {
15         cin>>v;
16         que.push(v);
17     }
18     int sum=0;
19     if(n==1) {
20         cout<<v<<endl;
21     }else {
22         while(que.size()!=1) {
23             int a=que.top();
24             que.pop();
25             int b=que.top();
26             que.pop();
27             sum+=a+b;
28             que.push(a+b);
29         }
30         cout<<sum<<endl;
31     }
32     return 0;
33 }
View Code

 

Guess you like

Origin www.cnblogs.com/wuliking/p/12670270.html