POJ 3253 Fence Repair (Analog Huffman tree + priority queue)

Source: Internet
Author: User

Test instructions: If you want to cut a wood bar of length A to cost a, ask for the minimum cost of cutting out the required n bars.

Idea: Simulate the Huffman tree, select the minimum two numbers at a time to add the results, and then add the two numbers and join the queue.

Note the use of priority_queue, prototypes:

1 priority_queue<type> q;   2 priority_queue<type,deque<type>,comp> q;  
View Code

Where type is the type, comp is the comparison struct, and the comparison function is its bracket overload, such as the comp structure of the type int from small to large:

1 struct Comp   2 {  3     booloperator() (const ll& A,const Const  4     {  5         return a>b;   6     }  7 };  
View Code

It is also important to note that there is no direct while (!q.empty ()), because each time you select two numbers, if the queue has only one number to take two numbers, then the program will explode, should use a while (1) loop, when the number is taken out immediately determine if (Q.empty ()) Break

Cannot directly simulate Huffman tree, direct Huffman will time out.

1 #pragmaComment (linker, "/stack:1024000000,1024000000")2#include <iostream>3#include <cstdio>4#include <cstring>5#include <cmath>6#include <math.h>7#include <algorithm>8#include <queue>9#include <Set>Ten#include <bitset> One#include <map> A#include <vector> -#include <stdlib.h> -#include <stack> the using namespacestd; - #definePI ACOs (-1.0) - #defineMax (a) (a) > (b)? (a): (b) - #defineMin (a) (a) < (b)? (a): (b) + #definell Long Long - #defineEPS 1e-10 + #defineMOD 1000000007 A #defineN 1000000 at #defineINF 1e12 - ll N; - structcomp{ -    BOOL operator()(Constll &a,Constll &b)Const{ -       returnA>b; -    } in }; - intMain () to { +     while(SCANF ("%i64d", &n) = =1){ -Priority_queue<ll,deque<ll>,comp>Q; the        for(intI=1; i<=n;i++){ * ll X; $scanf"%i64d",&x);Panax Notoginseng Q.push (x); -       } thell ans=0; +        while(1){ All l=q.top (); Q.pop (); the          if(Q.empty ()) Break; +ll r=q.top (); Q.pop (); -ans+=L; $ans+=R; $Q.push (L +R); -       } -printf"%i64d\n", ans); the    } -     return 0;Wuyi}
View Code

POJ 3253 Fence Repair (Analog Huffman tree + priority queue)

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.