poj3253Fence Repair(哈夫曼树)

来源:互联网 发布:淘宝网免单 编辑:程序博客网 时间:2024/05/27 00:44

Fence Repair
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 50638 Accepted: 16615
Description

Farmer John wants to repair a small length of the fence around the pasture. He measures the fence and finds that he needs N (1 ≤ N ≤ 20,000) planks of wood, each having some integer length Li (1 ≤ Li ≤ 50,000) units. He then purchases a single long board just long enough to saw into the N planks (i.e., whose length is the sum of the lengths Li). FJ is ignoring the “kerf”, the extra length lost to sawdust when a sawcut is made; you should ignore it, too.

FJ sadly realizes that he doesn’t own a saw with which to cut the wood, so he mosies over to Farmer Don’s Farm with this long board and politely asks if he may borrow a saw.

Farmer Don, a closet capitalist, doesn’t lend FJ a saw but instead offers to charge Farmer John for each of the N-1 cuts in the plank. The charge to cut a piece of wood is exactly equal to its length. Cutting a plank of length 21 costs 21 cents.

Farmer Don then lets Farmer John decide the order and locations to cut the plank. Help Farmer John determine the minimum amount of money he can spend to create the N planks. FJ knows that he can cut the board in various different orders which will result in different charges since the resulting intermediate planks are of different lengths.

Input

Line 1: One integer N, the number of planks
Lines 2..N+1: Each line contains a single integer describing the length of a needed plank
Output

Line 1: One integer: the minimum amount of money he must spend to make N-1 cuts
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).

题意:农夫要锯木板,但是没有工具,于是借别人的用,锯一次要木板的长度的钱,于是请你设计一个最小的花费方法。

贪心里面的哈夫曼算法,假设有7个小段1,7,5,3,8,2,6,
首先将其排序,从小到大,每次取两个最小的组成一个新的元素,最后若总数量为奇数则只剩一个,若总数量为偶数,则不剩。。。。啊哈哈

即形成如下的树:

这里写图片描述
1.先连接1,2,得到权值3
2.3和3形成新的最小权值6,
3.6和5形成新的最小权值11.
。。。。。。
。。。。。
最后将13和19形成根节点权值32

树完成。
在这里没有用哈夫曼树的代码,用vector优先队列,从小到大排序,每次取两个top元素组成新的元素,然后sum统计,再将新的元素加入队列,循环到最后,统计出来的sum就是我们所求的根节点的权值也就是最小总花费了。

代码:

#include <cstdlib>#include <iostream>#include <deque>#include <algorithm>#include <queue>#include <vector>#include <cstdio>using namespace std;struct cmp{     bool operator ()(int x, int y)    {        return x>y; // x小的优先级高      //也可以写成其他方式,如: return p[x] > p[y];表示p[i]小的优先级高}};priority_queue<int, vector<int>, cmp>q;int main(){    int n,num;    int a[20010];    //priority_queue<int, vector<int>, cmp>q;    //priority_queue<int,vector<int>,greater<int> >q;    scanf("%d",&n);    for(int i=0;i<n;i++)    {        scanf("%d",&num);        q.push(num);    }    long long sum=0;    if(q.size()==1)    {        sum+=q.top();        q.pop();    }    while(q.size()>1)    {        int a=q.top();        q.pop();        int b=q.top();        q.pop();        int c=a+b;        sum+=c;        q.push(c);    }   printf("%I64d\n",sum);    return 0;}
原创粉丝点击