7-7 修理牧场 (30分)

农夫要修理牧场的一段栅栏,他测量了栅栏,发现需要N块木头,每块木头长度为整数L​i​​ 个长度单位,于是他购买了一条很长的、能锯成N块的木头,即该木头的长度是L​i​​ 的总和。

但是农夫自己没有锯子,请人锯木的酬金跟这段木头的长度成正比。为简单起见,不妨就设酬金等于所锯木头的长度。例如,要将长度为20的木头锯成长度为8、7和5的三段,第一次锯木头花费20,将木头锯成12和8;第二次锯木头花费12,将长度为12的木头锯成7和5,总花费为32。如果第一次将木头锯成15和5,则第二次锯木头花费15,总花费为35(大于32)。

请编写程序帮助农夫计算将木头锯成N块的最少花费。

输入格式:

输入首先给出正整数N(≤10^​4),表示要将木头锯成N块。第二行给出N个正整数(≤50),表示每段木块的长度。

输出格式:

输出一个整数,即将木头锯成N块的最少花费。

输入样例:

8
4 5 1 2 1 3 1 1

输出样例:

49

最基本的哈夫曼树,注意建树的时候选取两个最小键值,要用小顶堆,遍历的方法会超时

源码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define Max 10001
#define MinData -1
typedef char **HuffmanCode;
typedef struct {
    int weight;
    int parent;
    int lchild;
    int rchild;
} HTNode, *HuffmanTree;
typedef struct DataTree *Data;
struct DataTree{
    int weight;
    int index;
};
typedef struct treenode *Heap;
struct treenode {
    Data member;
    int size;/*堆中的元素个数*/
};
Heap CreateHeap(void) {
    /*建立小顶堆*/
    Heap H = (Heap) malloc(sizeof(struct treenode));
    H->member = (Data) malloc(Max * sizeof(struct DataTree));
    H->size = 0;
    H->member[0].weight= MinData;
    H->member[0].index=-1;
    return H;
}

void InsertHeap(Heap H, int x,int index)
{
   int i=++H->size;
   for(;H->member[i/2].weight>x;i/=2)
       H->member[i]=H->member[i/2];
   H->member[i].weight=x;
   H->member[i].index=index;
}
void DeletTwoHeap(Heap H,int &s1,int &s2)
{
    int parent,child;
    s1=H->member[1].index;
    int x=H->member[H->size].weight;
    int deletindex=H->member[H->size].index;
    H->size--;
    //下滤
    for(parent=1;parent*2<=H->size;parent=child)
    {
        child=parent*2;
        if(child!=H->size&&H->member[child].weight>H->member[child+1].weight)
            child++;
        if(x>H->member[child].weight)
        H->member[parent]=H->member[child];
        else
            break;
    }
    H->member[parent].weight=x;
    H->member[parent].index=deletindex;

    s2=H->member[1].index;
    x=H->member[H->size].weight;
    deletindex=H->member[H->size].index;
    H->size--;
    //下滤
    for(parent=1;parent*2<=H->size;parent=child)
    {
        child=parent*2;
        if(child!=H->size&&H->member[child].weight>H->member[child+1].weight)
            child++;
        if(x>H->member[child].weight)
            H->member[parent]=H->member[child];
        else
            break;
    }
    H->member[parent].weight=x;
    H->member[parent].index=deletindex;
}
//用这种方法会造成超时,必须建立小顶堆
/*void SelectTwoMin(int upbound, HuffmanTree HT, int &s1, int &s2) {
    int min1, min2;
    min1 = min2 = 0;
    int i;
    HT[0].weight = Max;
    for (i = 1; i < upbound; i++) {
        if (HT[i].parent == 0 && HT[i].weight < HT[min1].weight)
            min1 = i;
    }
    for (i = 1; i < upbound; i++) {
        if (HT[i].parent == 0 && HT[i].weight < HT[min2].weight && i != min1)
            min2 = i;
    }
    s1 = min1;
    s2 = min2;
}*/

void CreateHufuman(HuffmanTree HT, int index,Heap H) {
    int s1, s2;
    DeletTwoHeap(H,s1,s2);
    //取出两个最小的结点
    HT[index].lchild = s1;
    HT[index].rchild = s2;
    HT[index].weight = HT[s1].weight + HT[s2].weight;
    HT[s1].parent = HT[s2].parent = index;
    InsertHeap(H,HT[index].weight,index);
    //将新的结点插入
}

long long Get_price(HuffmanTree HT, int n) {
    long long price = 0;
    for (int i = 1; i <= n; i++) {
        int lenth = 0;
        for (int j = i; HT[j].parent; j = HT[j].parent)
            lenth++;
        price += lenth * HT[i].weight;
    }
    return price;
}

int main() {
    int N;
    scanf("%d", &N);
    int M = 2 * N - 1;
    /*哈夫曼树建立*/
    HuffmanTree HT = (HuffmanTree) malloc((M + 1) * sizeof(HTNode));
    /*小顶堆建立*/
    Heap H=CreateHeap();
    /*下标从1开始*/
    for (int i = 1; i <= M; i++) {
        if (i <= N) {
            int weight;
            scanf("%d", &weight);
            InsertHeap(H,weight,i);
            //插入小顶堆
            HT[i].weight = weight;
            HT[i].parent = HT[i].lchild = HT[i].rchild = 0;
        } else {
            HT[i].rchild = HT[i].lchild = HT[i].weight = HT[i].parent = 0;
            CreateHufuman(HT, i,H);
            //i > N 哈夫曼结点建立
        }
    }

    /*计算价格*/
    long long price;
    price = Get_price(HT, N);
    printf("%ld\n", price);

    return 0;
}


发布了95 篇原创文章 · 获赞 12 · 访问量 2327
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 黑客帝国 设计师: 上身试试

分享到微信朋友圈

×

扫一扫,手机浏览