本文主要是介绍POJ 3253 修理栅栏 - (贪心 Huffman),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题目链接:http://poj.org/problem?id=3253
Huffman
#include <stdio.h>
#include <algorithm>
#include <functional>
#include <queue>
using namespace std;
//POJ 3253
int main() {int n;scanf("%d", &n);int *length = new int[n];for (int i = 0; i < n; ++i)scanf("%d", &length[i]);long long cost = 0;priority_queue<int, vector<int>, greater<int>> pq;for (int i = 0; i < n; ++i)pq.push(length[i]);while (pq.size() > 1) {int top1 = pq.top();pq.pop();int top2 = pq.top();pq.pop();cost += top1 + top2;pq.push(top1 + top2);}printf("%lld", cost);return 0;
}
这篇关于POJ 3253 修理栅栏 - (贪心 Huffman)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!