首页 > 其他 > 详细

POJ - 3253 Fence Repair

时间:2014-03-03 10:31:19      阅读:463      评论:0      收藏:0      [点我收藏+]

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

思路:利用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;
}



POJ - 3253 Fence Repair,布布扣,bubuko.com

POJ - 3253 Fence Repair

原文:http://blog.csdn.net/u011345136/article/details/20284369

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!