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.