POJ 3253

来源:互联网 发布:网络拓扑结构连线最短 编辑:程序博客网 时间:2024/06/08 16:42

题意:要将一块很长的木板切割成N块。准备切成L1,L2,L3……LN,每次切断木板时,需要的开销为木板的长度。要求出切割完最小的开销。

求最短的带全路径长度,利用哈夫曼编码的思想贪心的求得结果。

#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#include <vector>#include <map>#include <queue>using namespace std;typedef long long ll;int main(){    int n;    while(~scanf("%d",&n)){        priority_queue<ll>q;        ll temp;        for(int i = 0;i < n;i++){            scanf("%I64d",&temp);            q.push(-temp);        }        ll ans = 0;        ll t1,t2;        while(!q.empty()){            t1 = -q.top();            q.pop();            if(q.empty())                break;            t2 = -q.top();            q.pop();            ans += t1+t2;            //printf("%d ",ans);            q.push(-(t1+t2));        }        printf("%I64d\n",ans);    }    return 0;}


0 0
原创粉丝点击