POJ 3253 Fence Repair

链接:http://poj.org/problem?id=3253

每次都找出最短的两个合并..优先队列

#include <iostream>
#include<queue>
using namespace std;

int main()
{
    int n;
    int i;
    int t;
    int a,b;
    long long  ans;
    priority_queue< int ,vector<int>,greater<int> > que;
    while(cin>>n)
    {
        ans=0;
        for(i=0;i<n;i++)
        {
            cin>>t;
            que.push(t);
        }
        while(que.size()>1)
        {
            a=que.top();
            que.pop();
            b=que.top();
            que.pop();
            ans+=a+b;
            que.push(a+b);
        }

        cout<<ans<<endl;


    }

    return 0;
}


 

原文地址:https://www.cnblogs.com/frankM/p/4399499.html