Fence Repair POJ - 3253 最小优先队列,哈弗曼树
2017-07-15 19:18
435 查看
首先拿用例来说明一下题意
给个一根长度为21的模板,
目标是把它分为三块,长度分别是8,5,8
切割的时候先把他切割成长度为8和13的,消耗21,
然后再把长度为13的模板切割成长度为5和8的,消耗13
然后总消耗就是21+13=34
求最小总消耗
我用的是哈弗曼树来实现,需要注意的是输入n==1,也就是分成一块的时候,这时候直接弹出队列并输出木板长度,否则根据哈夫曼计算总消耗并输出
下面是代码
给个一根长度为21的模板,
目标是把它分为三块,长度分别是8,5,8
切割的时候先把他切割成长度为8和13的,消耗21,
然后再把长度为13的模板切割成长度为5和8的,消耗13
然后总消耗就是21+13=34
求最小总消耗
我用的是哈弗曼树来实现,需要注意的是输入n==1,也就是分成一块的时候,这时候直接弹出队列并输出木板长度,否则根据哈夫曼计算总消耗并输出
下面是代码
#include<iostream> #include<cstdio> #include<algorithm> #include<queue> using namespace std; int main() { int n; long long int sum; priority_queue<int, vector<int>, greater<int> > p;//最小优先队列,默认是最大 while(cin>>n) { int x; sum=0; for(int i=1; i<=n; ++i) { cin>>x; p.push(x); } if(n==1) { sum=p.top(); p.pop(); } else { int temp1,temp2; while(p.size()>1) { temp1=p.top(); p.pop(); temp2=p.top(); p.pop(); int temp; temp=temp1+temp2; p.push(temp); sum+=temp; } p.pop(); } cout<<sum<<endl; } }
相关文章推荐
- POJ—3253—Fence Repair—【贪心】【优先队列】
- Fence Repair POJ - 3253 (贪心!!哈夫曼最小二叉树)
- POJ 3253-Fence Repair(哈夫曼树-最小值优先队列)
- poj 3253 最小堆 优先队列 哈夫曼树
- POJ 3253 Fence Repair(STL之优先队列)
- POJ 3253 Fence Repair 题解(优先队列实现哈夫曼树)
- poj 3253 Fence Repair(Huffman问题,优先队列priority_queue)
- 刷题——Fence Repair POJ - 3253
- poj 3253 Fence Repair(优先队列+哈夫曼树)
- 贪心法 Fence Repair POJ 3253 霍夫曼(Huffman)编码原理 acmclub 12326
- 【原创】【合并类贪心】POJ 3253 1862 2709; Fence Repair && Stripies && Painter
- POJ 3253 Fence Repair Huffman
- POJ 3253 Fence Repair(优先队列,哈夫曼树,模拟)
- poj 3253 Fence Repair (STL优先队列)
- 优先队列 POJ 3253 Fence Repair
- poj1789之最小优先队列
- 20140822 【 优先队列 】 POJ 3253 Fence Repair
- POJ 3253 优先队列 Fence Repair
- poj 3253 Fence Repair greater<int>
- POJ 3253 Fence Repair C++ STL multiset 可解