NewCodeTemplate
This commit is contained in:
24
模板/树/Huffman最小代价.cpp
Normal file
24
模板/树/Huffman最小代价.cpp
Normal file
@@ -0,0 +1,24 @@
|
||||
#include <iostream>
|
||||
#include <queue>
|
||||
using namespace std;
|
||||
int main(){
|
||||
priority_queue<int, vector<int>, greater<int>> pq;
|
||||
int n;
|
||||
cin >> n;
|
||||
int tmp;
|
||||
int sum = 0;
|
||||
for(int i = 0; i < n; i++){
|
||||
cin >> tmp;
|
||||
sum += tmp;
|
||||
pq.push(tmp);
|
||||
}
|
||||
long long ans = 0;
|
||||
while(pq.size() > 1){
|
||||
int x = pq.top(); pq.pop();
|
||||
int y = pq.top(); pq.pop();
|
||||
pq.push(x + y);
|
||||
ans += (x + y);
|
||||
}
|
||||
cout << ans << endl;
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user