# Hackerrank "Prim ' s (MST): Special subtree"

Source: Internet
Author: User

An intuitive Prim algorithm impl.

`#include <vector>#include<iostream>#include<queue>#include<unordered_map>#include<unordered_set>using namespacestd;structedge{Edge (): s (0), T (0), D (0{} Edge (unsigned RS, unsigned rt, unsigned rd): s (rs), T (RT), D (RD) {} unsigned s;    unsigned t;    unsigned D; BOOL operator()(ConstEdge &e1,ConstEdge &E2) {        returne1.d >e2.d; }};intMain () {LongN, M; CIN >> N >>m; //From -to-and (length, id)unordered_map<unsigned, unordered_map<unsigned, unsigned>>G;  for(inti =0; I < m; i++) {Unsigned A, B, D; CIN>> a >> b >>D; G[A][B]= G[b][a] =D; } unsigned s; CIN>>s; Unordered_set<unsigned> Pickedset;//linked node Inxpriority_queue<edge, Vector<edge>, edge>Q;  for(Auto &Kv:g[s])    {Q.push (Edge (S, Kv.first, Kv.second));    } pickedset.insert (s); unsignedLong LongRET =0;  while(!Q.empty ()) {Edge picked=Q.top ();        Q.pop (); if(Pickedset.count (PICKED.T))Continue;        Pickedset.insert (PICKED.T); //Add new Choices         for(Auto &kv:g[picked.t])        {Q.push (Edge (picked.t, Kv.first, Kv.second)); } ret+=Picked.d; } cout<< ret <<Endl; return 0;}`

Hackerrank "Prim ' (MST): Special subtree"

Related Keywords:

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.