poj 3253 Fence Repair

Fence Repair
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 36991   Accepted: 11970

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).

Source

USACO 2006 November Gold
这个题最简单的做法应该是贪心,只是今天的作业是练习哈夫曼树就用哈夫曼树写的,道理差不多,详见代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int l[20005];
typedef struct
{
    int l;
    int parent,lchild,rchild;
} HTNode,*HuffmanTree;      //哈夫曼节点,包括权值,父节点,左子树,右子树

void Select(HuffmanTree &h,int i,int& s1,int& s2)//从h树中找出从元素1到i-1之间权值最小的两个父节点为0的节点
{
    s1 = s2 = 0;//初始化s1s2值,先找到两个父节点为0的节点
    for(int k = 1; k < i; k++)
    {
        if(h[k].parent == 0)
        {
            s1 = k;
            if(!s2) s2 = s1;
            else break;
        }
    }

    if(h[s1].l > h[s2].l) swap(s1,s2);//确保s1元素小于s2
    for(int j = 1; j < i; j++)//遍历数组求出两个最小的权值节点
    {
        if(h[j].parent == 0 && j != s1 && j != s2)
        {
            if(h[j].l < h[s1].l)
            {
                s2 =  s1;
                s1 = j;
            }
            else if(h[j].l < h[s2].l)
            {
                s2 = j;
            }
        }
    }
}
void HuffmanCoding(HuffmanTree & h,int *w,int n)//构建哈夫曼树
{
    int s1,s2,i;
    if(n <=1) return;
    int m = 2 * n - 1;      //哈夫曼树一共会有2*n-1个节点
    h = (HuffmanTree)malloc((m+1) * sizeof(HTNode));//创建一个大小为哈夫曼树节点个数加1的数组,不用到0号元素所以多建一个
    for(i = 1; i<=m; ++i,++w)//初始化数组节点值
    {
        if(i <= n) h[i].l = *w;//1~n号节点存储叶节点,所以有初始权值即对应读入数据存储的值
        else h[i].l = 0;//n+1~m号节点为内部节点,暂时没有权值
        h[i].parent = h[i].lchild = h[i].rchild = 0;//此时树未建立没有父节点子节点的信息都为0
    }
    for(i = n + 1; i <= m; i++)//合并两个最小权值的元素存入内部节点
    {
        Select(h,i,s1,s2);//每次合成的节点也要算在下一次寻找的过程中,所以要从1~i-1号节点中找
        h[s1].parent = i;//存储内部节点的权值以及相关的父子节点信息
        h[s2].parent = i;
        h[i].lchild = s1;
        h[i].rchild = s2;
        h[i].l = h[s1].l + h[s2].l;
    }
}
void Path(HuffmanTree &h,int n)//内部节点求和即为最小开销
{
    int ans = 0;
    for(int j = n + 1; j <= 2 *n - 1; j++)
    {
        ans += h[j].l;
    }
    cout <<  ans << endl;
}
int main()
{
    ios::sync_with_stdio(false);
    HuffmanTree h;
    int n,temp;
    cin >> n;
    for(int  i  = 0; i < n; i++)
    {
        cin >> l[i];
    }
    HuffmanCoding(h,l,n);
    Path(h,n);
    return 0;
}


你可能感兴趣的:(ACM,哈夫曼树)