#10406. szjy_6_树_026

szjy_6_树_026

szjy_6_树_026

下面的代码用小根堆计算哈夫曼树的带权路径长度。程序输出是( )。

#include <iostream>
#include <queue>
#include <vector>
#include <functional>
using namespace std;

int main() {
priority_queue<int, vector<int>, greater<int>> q;
for (int x : {4, 5, 7, 10}) q.push(x);
int ans = 0;
while (q.size() > 1) {
int a = q.top(); q.pop();
int b = q.top(); q.pop();
ans += a + b;
q.push(a + b);
}
cout << ans;
}

{{ select(1) }}

  • 47
  • 51
  • 56
  • 60