POJ-1287 Networking---naked, no more naked MST.

Source: Internet
Author: User

Topic Links:

https://vjudge.net/problem/POJ-1287

Main topic:

Template

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <algorithm>5#include <cmath>6#include <queue>7#include <stack>8#include <map>9#include <sstream>Ten using namespacestd; OnetypedefLong Longll; A Const intMAXN = 3e5 +Ten; - Const intINF =1<< -; - intdir[4][2] = {1,0,0,1,-1,0,0,-1}; the intT, N, m, X; - structEdge - { -     intu, V, W; +     BOOL operator< (Constedge& a)Const -     { +         returnW <A.W; A     } at }; - Edge A[MAXN]; - intpar[ -], high[ -]; - //Initialize n elements - voidInitintN) - { in      for(inti =0; I < n; i++) -     { toPar[i] =i; +High[i] =0; -     } the } * //root of the query tree $ intFind (intx)Panax Notoginseng { -     returnPAR[X] = = x? X:PAR[X] = Find (par[x]);//Path Compression the } + voidUniteintXinty) A { thex =Find (x); +y =Find (y); -     if(x = = y)return; $     if(High[x] < high[y]) par[x] = y;//the height of Y is high, and the parent node of x is set to Y $     Else -     { -Par[y] =x; the         if(High[x] = = High[y]) high[x]++; -     }Wuyi } the BOOLSame (intXinty) - { Wu     returnFind (x) = =Find (y); - } About voidKruskal (intNintM//points N, number of sides M $ { -     intSum_mst =0;//MST weight Value -     intnum=0;//The number of edges that have been selected -Sort (A, A + M);//side to sort AInit (n);//initializing and checking the set +      for(inti =0; I < m; i++) the     { -         intU =a[i].u; $         intv =a[i].v; the         if(Find (U-1)! = Find (V-1))//The first subscript of the graph is 1, and the check set is 0 the         { the             //printf ("%d%d%d\n", u, V, A[I].W); theSum_mst + =A[I].W; -num++; inUnite (U-1, V-1); the         } the         if(Num >= N-1) Break; About     } the     //printf ("Weight of MST is%d\n", sum_mst); thecout<<sum_mst<<Endl; the } + intMain () - { the      while(Cin >> N &&N)Bayi     { theCIN >>m; the          for(inti =0; I < m; i++) -         { -CIN >> a[i].u >> a[i].v >>A[I].W; the         } the Kruskal (n, m); the     } the     return 0; -}

POJ-1287 Networking---naked, no more naked MST.

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.