poj3253:http://poj.org/problem?id=3253,简单的哈夫曼问题
注意此题的数值范围,最后结果用unsigned int或long保存,不然会数据会溢出,另外,注意n=1时,花费为0。
这里优先队列的用法,可以设置比较函数,重载()运算符,还有一种用来比较结构体的方法,重载>操作符。
#include <iostream> #include <fstream> #include <queue> using namespace std; struct cmp { bool operator() (unsigned int x,unsigned int y) { return x>y; } }; priority_queue<unsigned int,vector<unsigned int>,cmp> q; unsigned int ans=0; unsigned int total=0; void doit() { unsigned int sum=0; while(!q.empty()) { sum+=q.top(); q.pop(); if(!q.empty()) { sum+=q.top(); q.pop(); if(sum!=total) q.push(sum); } ans+=sum; sum=0; } } int main() { unsigned int n; // ifstream cin("1.txt"); cin>>n; if(1==n) { ans=0; } else { for(unsigned int i=0;i<n;i++) { unsigned int temp; cin>>temp; q.push(temp); total+=temp; } doit(); } cout<<ans<<endl; return 0; }
注意此题的数值范围,最后结果用unsigned int或long保存,不然会数据会溢出,另外,注意n=1时,花费为0。
这里优先队列的用法,可以设置比较函数,重载()运算符,还有一种用来比较结构体的方法,重载>操作符。
struct node { int x, y; friend bool operator < (node a, node b) { return a.x > b.x; //结构体中,x小的优先级高 } }; priority_queue<node>q;//定义方法
发表评论
-
2011清华考研机试题2
2011-08-09 14:56 907http://ac.jobdu.com/problem.php ... -
2011清华考研机试题1
2011-08-09 14:40 642题目描述 http://ac.jobdu.com/proble ... -
考研清华2011复试机考第三题
2011-08-09 13:51 1490题目描述 在某条线路上有N个火车站,有三种距离的路程,L1, ... -
poj3259 spfa解法
2011-08-08 19:59 1356同上题,不过改的spfa算法,注意每个节点进入队列的次数至多为 ... -
poj3259 bellman水题
2011-08-08 17:04 969poj3259http://poj.org/problem?i ... -
acm题目常用的预处理
2011-08-08 15:26 1163#include<iostream> #in ... -
poj1860
2011-08-08 14:06 704poj1860http://poj.org/problem?i ... -
water~9
2011-08-06 18:01 418poj2109http://poj.org/problem?i ... -
water~8
2011-08-06 17:22 573poj2027http://poj.org/problem?i ... -
water~7
2011-08-06 17:15 567poj1328http://poj.org/problem?i ... -
water~6
2011-08-06 14:27 706poj1088http://poj.org/problem?i ... -
water~5
2011-08-06 14:24 581poj1003http://poj.org/problem?i ... -
water~4
2011-08-06 14:09 636poj1004http://poj.org/problem?i ... -
water~3
2011-08-06 13:59 522poj2159http://poj.org/problem?i ... -
water~2
2011-08-06 12:10 533poj3299http://poj.org/problem?i ... -
water~1
2011-08-06 10:35 639poj1503http://poj.org/problem?i ... -
POJ3280 简单DP
2011-08-05 14:48 893poj3280:http://poj.org/problem? ...
相关推荐
北大POJ3253-POJ3253-Fence Repair【STL优先队列】 解题报告+AC代码
这道题目在于应用哈夫曼算法 其中涉及的排序算法可以直接调用库函数中的
POJ1523 - SPF 测试数据。 数据来源:Greater New York 2000 问题H
POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类
poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题...
POJ第1861题源码 POJ第1861题源码 POJ第1861题源码
北大POJ1159-Palindrome 解题报告+AC代码
C语言 poj npu 西工大 C语言Poj答案全完整打包,给有需要的朋友
LeetCode判断字符串是否循环 :bookmark_tabs:Plan 动态规划 背包问题 动态规划 POJ 3267 POJ 1260 POJ ...POJ ...POJ ...POJ ...POJ ...POJ ...POJ ...POJ ...POJ ...POJ ...POJ ...POJ ...POJ ...POJ ...POJ ...POJ ...POJ ...POJ ...3253 trie树 POJ 251
poj 3414解题报告poj 3414解题报告poj 3414解题报告poj 3414解题报告
poj分类poj分类poj分类poj分类
poj 1012解题报告poj 1012解题报告poj 1012解题报告poj 1012解题报告
poj 2329解题报告poj 2329解题报告poj 2329解题报告poj 2329解题报告
北大POJ2002-Squares 解题报告+AC代码
POJ1503解答 POJ1503解答,正确答案(已通过POJ)
POJ1048,加强版的约瑟夫问题 难度中等
poj 1659解题报告poj 1659解题报告poj 1659解题报告poj 1659解题报告
POJ1083的代码,POJ1083的代码,POJ1083的代码
poj 百练 题目分类 poj 百练 题目分类
POJ上的一道题目,自己写的代码,因为想下载别人的, 所以就放上了。