poj 哈夫曼树相关之3253 Fence Repair
看了别人的代码写的,用了优先队列的相关知识,不然自己做肯定就会写一堆类和指针了。
优先队列的相关知识:
http://blog.csdn.net/lethic/article/details/7773764
补充:
#include<queue>
priority_queue<int> q //是默认从大到小排列
#include<functional>
priority_queue<int,vector<int>,greater<int>> q //从小到大排列
priority_queue<int,vector<int>,less<int>> q //从大到小排列
struct node
{
friend bool operator<(node n1,node n2)
return n1.p<n2.p;//从大到小(与平时写的比较函数恰好相反)
int p;
}
388K 79MS
ac 代码:
#include<iostream>
#include<functional>
#include<queue>
using namespace std;
int main()
{
//freopen("1.txt", "r", stdin);
int n, i, j;
long long sum;
priority_queue<int, vector<int>, greater<int>>que;
cin >> n;
for (i = 1; i <= n; i++)
{
cin >> j;
que.push(j);
}
sum = 0;
while (que.size() > 1)
{
i = que.top();
que.pop();
j = que.top();
que.pop();
que.push(i + j);
sum += (i + j);
}
cout << sum << endl;
}