优先队列/POJ3253,哈夫曼树(求最小木板分割费用)
2011-06-04 10:43
621 查看
描述:一个需要 9 10 11三块木板,现在他有9+10+11长度的木板,但是分割L长度的木板费用就是L,因此求怎么分割费用最小
算法:构造哈夫曼树,队列中取出最小的两个数放在底层,它们的和入队,如此反复;可以利用优先队列priority_queue
优先队列priority_queue默认大的数先出队,因此需要重载小于号
Sample Input 3 8 5 8 Sample Output 34 Hint He wants to cut a board of length 21 into pieces of lengths 8, 5, and 8. The original board measures 8+5+8=21. The first cut will cost 21, and should be used to cut the board into pieces measuring 13 and 8. The second cut will cost 13, and should be used to cut the 13 into 8 and 5. This would cost 21+13=34. If the 21 was cut into 16 and 5 instead, the second cut would cost 16 for a total of 37 (which is more than 34).
#include<iostream> #include<queue> using namespace std; struct node { __int64 w; bool operator <(node b)const {return w>b.w;} }tmp; int n; int main() { int i; scanf("%d",&n); priority_queue<node>q; for(i=1;i<=n;i++) { scanf("%lld",&tmp.w); q.push(tmp); } __int64 ans=0; while(q.size()>1) { node a=q.top();q.pop(); node b=q.top();q.pop(); a.w+=b.w; ans+=a.w; q.push(a); } printf("%lld\n",ans); return 1; }
相关文章推荐
- (哈夫曼树10.3.1)POJ 3253 Fence Repair(计算带权路径长度最小的哈夫曼树)
- 【树的算法】之求分割木板最小开销
- POj 3253 Fence Repair(修农场栅栏,锯木板)(小根堆 + 哈弗曼建树得最小权值思想 )
- POJ 3253-Fence Repair(哈夫曼树-最小值优先队列)
- poj 3253 木板分割
- 最小费用-最大流---hdu1533
- 740. [网络流24题] 分配问题 费用流/求最大最小费用
- bzoj3876 支线剧情【上下界最小费用可行流】
- 霍夫曼树之切割木板最小总代价问题
- bzoj 3206: [Apio2013]道路费用【最小生成树+并查集】
- hdu5352 MZL's City(最小费用最大流问题)
- 最小乘车费用
- 最小费用最大流模板
- HDU3488Tour(KM最小费用圈覆盖)
- 最小的乘车费用
- poj 2195 最小费用最大流模板
- POJ 3270 Cow Sorting(置换最小费用)
- 图形分割算法<==>最小割<==>最大流<==>网络流问题
- (hdu step 6.1.2)Eddy's picture(在只给出二维坐标点的情况下,求让n个点连通的最小费用)
- hdu 6118 度度熊的交易计划 (最小费用最大流