Merging fruits (priority queue + or + Huffman)

Source: Internet
Author: User

Combined fruit time limit:1 Sec Memory limit:128 MB
submit:312 solved:113
[Submit] [Status] [Web Board] Description

Now there are n heaps of fruit, and the first heap has AI fruit. Now to merge these fruits into a heap, the cost of each merger is the total fruit count of the two piles of fruit. The minimum cost of merging all fruits.

Input

The first line contains an integer T (t<=50) that represents the number of data groups.
The first row of each group of data contains an integer n (2<=n<=1000) that represents the number of heaps of fruit.
The second line contains n positive integer ai (ai<=100), which represents the number of fruits per heap.

Output

Only one row per group of data represents the minimum consolidation cost.

Sample Input
241 2 3 453 5 2 1 4
Sample Output
1933
HINT

(priority queue + or + Huffman)

#include <stdio.h>#include<queue>using namespacestd;structnode{intA; FriendBOOL operator<(node Aa,node bb) {returnAa.a>BB.A; }};intMain () {intn,t,a,sum;    Node Bb,aa; scanf ("%d",&t);  while(t--) {priority_queue<node>Q; scanf ("%d",&N);  for(intI=0; i<n;i++) {scanf ("%d",&a); BB.A=A;        Q.push (BB); } Sum=0;  while(!Q.empty ()) {AA=q.top (); Q.pop ();//printf ("%d", aa.a);            if(n==1) Break; BB=q.top (); Q.pop (); N--; AA.A+=BB.A; Sum+=aa.a;        Q.push (AA); } printf ("%d\n", sum); }} /************************************************************** problem:1588 user:aking2015 language:c++ result:accepted time:92 Ms memory:1064 kb****************************************************************/

Merging fruits (priority queue + or + Huffman)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.