Prim algorithm for Hihocoder 1109 heap optimization

Source: Internet
Author: User

Topic Links: http://hihocoder.com/problemset/problem/1109, minimum spanning tree + heap optimization (priority queue).

Can use the priority queue, or you can manually simulate the heap, in order to practiced hand, I have tried both, priority queue or to be convenient, but the heap to be faster.

Learn the algorithm well, there is no gratuitous love, there is no reason to reduce the complexity of time.

 

Heap-Optimized code:

#include <iostream>#include<cstdio>#include<vector>#include<queue>#include<cmath>#include<string>#include<string.h>#include<algorithm>using namespacestd;#defineLL __int64#defineEPS 1e-8#defineINF 1e8#defineLson L, M, RT << 1#defineRson m + 1, R, RT << 1 | 1Const intMOD =2333333; Const intMAXN =100000+5;structEdge {//adjacency Table representation diagram    intV, W; BOOL operator< (ConstEdge &tmp)Const {        returnW <TMP.W; }} a[maxn];vector<Edge>E[MAXN];intVIS[MAXN], DIST[MAXN];voidPushup (intRtintR) {        //update from bottom up with RT as Root, R to right boundary heap    inti = r >>1;  while(I >=RT) {        if(A[r] <A[i])            {Swap (A[r], a[i]); R=i; I>>=1; } Else {             Break; }    }}voidPushdown (intRtintR) {        //update from top to bottom with RT as Root, R to right boundary heap    inti = RT <<1;  while(I <=r) {if(A[i].w > A[i +1].w) I++; if(A[i] <A[rt])            {Swap (A[i], a[rt]); RT=i; I<<=1; } Else {             Break; }    }}intPrim (intS,intN) {    intTot, ans;//tot represents the number of elements in the heaptot = ans =0; memset (Vis,0,sizeof(VIS));  for(inti =1; I <= N; i++) Dist[i]=INF; a[++TOT].V =s; A[TOT].W=0;  while(Tot >=1) {Edge u= a[1]; Swap (a[1], A[tot]); A[tot--].W =INF; Pushdown (1, tot); if(VIS[U.V])Continue; Ans+=U.W; VIS[U.V]=1;  for(inti =0; I < e[u.v].size (); i++) {            intj =e[u.v][i].v; if(Dist[j] >E[U.V][I].W) {Dist[j]=E[U.V][I].W; if(!Vis[j]) {a[++tot] =E[u.v][i]; Pushup (1, tot); }            }        }    }    returnans;}intMain () {intN, M, U, V, W; scanf ("%d%d", &n, &m);  while(m--) {scanf (" %d%d%d", &u, &v, &W); Edge tmp={V, w};        E[u].push_back (TMP); TMP.V=u;    E[v].push_back (TMP); } printf ("%d\n", Prim (1, N)); return 0;}

Priority Queue Optimization (note the details of overloaded operator places):

#include <iostream>#include<cstdio>#include<vector>#include<queue>#include<cmath>#include<string>#include<string.h>#include<algorithm>using namespacestd;#defineLL __int64#defineEPS 1e-8#defineINF 1e8#defineLson L, M, RT << 1#defineRson m + 1, R, RT << 1 | 1Const intMOD =2333333; Const intMAXN =100000+5;structEdge {intV, W; BOOL operator< (ConstEdge &tmp)Const {        returnW >TMP.W; }} a[maxn];vector<Edge>E[MAXN];intVIS[MAXN], DIST[MAXN];intPrim (Edge S,intN) {Priority_queue<Edge>que; intAns =0;  for(inti =1; I <= N; i++) {Dist[i]=INF; Vis[i]=0;    } que.push (s);  while(!Que.empty ()) {Edge U=Que.top ();        Que.pop (); if(VIS[U.V])Continue; VIS[U.V]=1; Ans+=U.W;  for(inti =0; I < e[u.v].size (); i++) {            intj =e[u.v][i].v; if(Dist[j] > E[U.V][I].W &&!Vis[j]) {Dist[j]=E[U.V][I].W;            Que.push (E[u.v][i]); }        }    }    returnans;}intMain () {intN, M, U, V, W; scanf ("%d%d", &n, &m);  while(m--) {scanf (" %d%d%d", &u, &v, &W); Edge tmp={V, w};        E[u].push_back (TMP); TMP.V=u;    E[v].push_back (TMP); } Edge tmp= {1,0}; printf ("%d\n", Prim (TMP, n)); return 0;}

Prim algorithm for Hihocoder 1109 heap optimization

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.