POJ - 3253 Fence Repair

来源:互联网 发布:淘宝分享送红包 编辑:程序博客网 时间:2024/05/17 07:49

题意:要将一个木板分成几个小的木板,每次的费用是当前木板的大小,求得到所有木板的最小花费

思路:利用Huffman思想,采用优先队列

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <queue>using namespace std;struct node{    long long len;    bool operator<(const node &a)const{        return len > a.len;    }};priority_queue<node> s;int main(){    int n;    node a;    scanf("%d",&n);    for (int i = 0; i < n; i++){        scanf("%lld",&a.len);        s.push(a);    }    long long ans = 0;    while (s.size() > 1){        node x = s.top();s.pop();        node y = s.top();s.pop();        x.len += y.len;        ans += x.len;        s.push(x);    }    printf("%lld\n",ans);    return 0;}



0 0
原创粉丝点击