poj 3253 Fence Repair (哈夫曼树 优先队列)

题目:http://poj.org/problem?id=3253

没用long long wrong 了一次

 1 #include <iostream>
 2  #include<cstdio>
 3  #include<cstring>
 4  #include<cstdlib>
 5  #include<stack>
 6  #include<queue>
 7  #include<iomanip>
 8  #include<cmath>
 9  #include<algorithm>
10  #include<map>
11  using namespace std;
12  
13  int main()
14  {
15      long long n,i,j,a,sum,cnt;//或者 __int64 n,i,j,a,sum,cnt;
16      priority_queue<int,vector<int>,greater<int> >pq;
17      cin>>n;
18      for(i=0; i<n; i++)
19      {
20          cin>>a;
21          pq.push(a);
22      }
23      sum=0;
24      while(!pq.empty())
25      {
26          cnt=pq.top();
27          pq.pop();
28          if(pq.empty())
29          break;
30          cnt+=pq.top();
31          sum+=cnt;
32          pq.pop();
33          pq.push(cnt);
34      }
35      cout<<sum<<endl;
36      return 0;
37  }
38  

 

posted @ 2013-08-19 20:50  水门  阅读(187)  评论(0编辑  收藏  举报