POJ 3253 Fence Repair "two fork Heap"

Source: Internet
Author: User

Test instructions: Give n planks, need to connect them together, each connection is spent on their length of the sum, ask at least how much money.

The same as the previous fruit merge, but the problem with a long long

Learn the code of the handwritten binary heap, and then well understand = =

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cmath>5#include <stack>6#include <vector>7#include <map>8#include <queue>9#include <algorithm>Ten #definemod=1e9+7; One using namespacestd; A  -typedefLong LongLL; - Const intmaxn=50005; the intL[MAXN]; - intN,h,minn; - LL ans; -  + voidHeap_sort (intx) {//This is a little Gan . -     intLG,LR;//LG represents the left node of the head, and LR represents the right node of the head +      while((x<<1) <=h) { Alg=x<<1; atLr= (x<<1)+1; -          -         if(lr<=h&&l[lg]>L[LR]) -lg=LR; -         if(l[x]>L[LG]) { - swap (L[X],L[LG]); inx=LG; -         } to         Else +            Break;  -     }     the } *  $ intMain () {Panax Notoginseng     inti; -      while(SCANF ("%d", &n)! =EOF) { the          for(i=1; i<=n;i++) +scanf"%d",&l[i]); A          theH=N; +          for(i=n/2;i>0; i--) - Heap_sort (i); $          $ans=0; -          -          while(h>1){ theminn=l[1];//Take out the current root, which is the current smallest number -l[1]=l[h];h--;//Put the last number in the first place .WuyiHeap_sort (1);//Descent Operation the              -minn+=l[1];//after the descent, the current root is removed, which is the smallest number Wul[1]=minn;//Add in the current and new pile -Heap_sort (1);//Descent Operation About              $ans+=Minn; -         } -          -printf"%i64d\n", ans); A     } +     return 0; the}
View Code

Change the previous code slightly to use the priority queue

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cmath>5#include <stack>6#include <vector>7#include <map>8#include <queue>9#include <algorithm>Ten #definemod=1e9+7; One using namespacestd; A  -typedefLong LongLL; -  the intMain () { -      -     intN,a; -      while(SCANF ("%d", &n)! =EOF) { +priority_queue<int,vector<int>,greater<int> >PQ; -LL ans=0; +          A          for(intI=1; i<=n;i++){ atscanf"%d",&a); - Pq.push (a); -     } -      -      while(Pq.size () >1){ -         intx=pq.top ();p q.pop (); in         inty=pq.top ();p q.pop (); -ans+=x+y; toPq.push (x+y); +     } -printf"%i64d\n", ans); the     } *     return 0; $}
View Code

POJ 3253 Fence Repair "two fork Heap"

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.