优先级队列  + 哈夫曼树

来源:互联网 发布:pubmed数据库 编辑:程序博客网 时间:2024/05/20 22:02

懒省事的小明

时间限制:3000 ms  |  内存限制:65535 KB
难度:3
描述
      小明很想吃果子,正好果园果子熟了。在果园里,小明已经将所有的果子打了下来,而且按果子的不同种类分成了不同的堆。小明决定把所有的果子合成一堆。 因为小明比较懒,为了省力气,小明开始想点子了:
  每一次合并,小明可以把两堆果子合并到一起,消耗的体力等于两堆果子的重量之和。可以看出,所有的果子经过n-1次合并之后,就只剩下一堆了。小明在合并果子时总共消耗的体力等于每次合并所耗体力之和。 
  因为还要花大力气把这些果子搬回家,所以小明在合并果子时要尽可能地节省体力。假定每个果子重量都为1,并且已知果子的种类数和每种果子的数目,你的任务是设计出合并的次序方案,使小明耗费的体力最少,并输出这个最小的体力耗费值。 
  例如有3种果子,数目依次为1,2,9。可以先将1、2堆合并,新堆数目为3,耗费体力为3。接着,将新堆与原先的第三堆合并,又得到新的堆,数目为12,耗费体力为12。所以小明总共耗费体力=3+12=15。可以证明15为最小的体力耗费值。
输入
第一行输入整数N(0<N<=10)表示测试数据组数。接下来每组测试数据输入包括两行,第一行是一个整数n(1<=n<=12000),表示果子的种类数。第二行包含n个整数,用空格分隔,第i个整数ai(1<=ai<=20000)是第i种果子的数目。
输出
每组测试数据输出包括一行,这一行只包含一个整数,也就是最小的体力耗费值。
样例输入
13 1 2 9
样例输出
15


/* nyoj-55 懒省事的小明
哈夫曼树, 优先队列实现 

*/

#include <iostream>
#include <stdio.h>
#include <queue>

using namespace std;

priority_queue<int, vector<int>, greater<int> >q;

int main()
{
int loop, n, t;
scanf("%d", &loop);
while(loop --)
{
while(!q.empty())
q.pop();
scanf("%d", &n);
while(n --)
{
scanf("%d", &t);
q.push(t);
}
long long ans = 0;
while(q.size() > 1)
{
t = q.top();
q.pop();
t += q.top();
q.pop();
ans += t;
q.push(t);
}
cout << ans << endl;
}
return 0;
}
#include <iostream>#include <stdio.h>#include <queue>using namespace std;priority_queue<int, vector<int>, greater<int> >q;int main(){int loop, n, t;scanf("%d", &loop);while(loop --){while(!q.empty())q.pop();scanf("%d", &n);while(n --){scanf("%d", &t);q.push(t);}long long ans = 0;while(q.size() > 1){t = q.top();q.pop();t += q.top();q.pop();ans += t;q.push(t);}cout << ans << endl;}return 0;}


哈夫曼树:(最优二叉树)
每次找节点数值最小的两个点,将其合并成一个新节点, 如此重复,知道只剩下一个节点为止。

优先级队列:

empty() 如果队列为空返回真

pop() 删除对顶元素

push() 加入一个元素

size() 返回优先队列中拥有的元素个数

top() 返回优先队列对顶元素

在默认的优先队列中,优先级高的先出队。在默认的int型中先出队的为较大的数。

使用方法:

头文件:

#include <queue>

声明方式:

1、普通方法:

priority_queue<int>q;
//通过操作,按照元素从大到小的顺序出队

2、自定义优先级:

        struct cmp{operator bool ()(int x, int y){return x > y; // x小的优先级高//也可以写成其他方式,如: return p[x] > p[y];表示p[i]小的优先级高}};


priority_queue<int, vector<int>, cmp>q;//定义方法
//其中,第二个参数为容器类型。第三个参数为比较函数。
//若不自定义则 声明  priority_queue<int, vector<int>, greater<int> >q;
// 第二个参数为容器类型。第三个参数为比较函数。队列按从小到大排列

3、结构体声明方式:

struct node
{
int x, y;
friend bool operator < (node a, node b)
{
return a.x > b.x; //结构体中,x小的优先级高
}
};
priority_queue<node>q;//定义方法
//在该结构中,y为值, x为优先级。
//通过自定义operator<操作符来比较元素中的优先级。

代码实例:

#include<iostream>#include<stdio.h>#include<queue>using namespace std;struct node{    friend bool operator< (node n1, node n2)    {        return n1.priority < n2.priority;    }    int priority;    int value;};int main(){    const int len = 5;    int i;    int a[len] = {3,5,9,6,2};    //示例1    priority_queue<int> qi;    for(i = 0; i < len; i++)        qi.push(a[i]);    for(i = 0; i < len; i++)    {        cout<<qi.top()<<" ";        qi.pop();    }    cout<<endl;        //示例2    priority_queue<int, vector<int>, greater<int> >qi2;    //第二个参数为容器类型。第三个参数为比较函数。     for(i = 0; i < len; i++)        qi2.push(a[i]);    for(i = 0; i < len; i++)    {        cout<<qi2.top()<<" ";        qi2.pop();    }    cout<<endl;        //示例3    priority_queue<node> qn;    node b[len];    b[0].priority = 6; b[0].value = 3;     b[1].priority = 9; b[1].value = 1;     b[2].priority = 2; b[2].value = 4;     b[3].priority = 8; b[3].value = 2;     b[4].priority = 1; b[4].value = 5;     for(i = 0; i < len; i++)        qn.push(b[i]);    cout<<"优先级"<<'\t'<<"值"<<endl;    for(i = 0; i < len; i++)    {        cout<<qn.top().priority<<'\t'<<qn.top().value<<endl;        qn.pop();    }    return 0;}