题目:http://codeup.cn/problem.php?id=21142&csrf=bvHcDf5kc87m5weAdrD1yXRXLa23xYvG
可以不用求出树形结构的哈夫曼带权路径,利用了priority_queue,代码比较简介
代码
#include<algorithm>#include<queue>#include<iostream>#include<cstdio>using namespace std;typedef long long LL;priority_queue<LL, vector<LL>, greater<LL> >q;int main(){int n;LL temp, ans = 0;scanf("%d", &n);for(int i = 0; i < n; i++){scanf("%lld", &temp);q.push(temp);}while(q.size() > 1){LL a = q.top();q.pop();LL b = q.top();q.pop();q.push(a + b);ans += a + b;}printf("%lld", ans);return 0;}
