POJ-1861 Network---minimum spanning tree

Source: Internet
Author: User

Topic Links:

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

Main topic:

There are companies that need to be connected between companies. gives the lengths of which companies can connect and connect edges. The largest edge in the minimum spanning tree, the number of edges of the minimum spanning tree, and the output of a viable minimum spanning tree.

Ideas:

Naked Kruskal.

The output here is the maximum edge and the number of edges, not the weights, and the sample is wrong

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 = 1e5 +Ten; - Const intINF =1<< -; - intdir[4][2] = {1,0,0,1,-1,0,0,-1}; the intT, N, M; - structEdge - { -     intu, V, W; +     BOOL operator< (Constedge& a)Const -     { +         returnW <A.W; A     } at }; -Edge A[MAXN], ans[1010]; - inttot, Maxx; - intpar[1010], high[1010]; - //Initialize n elements - voidInitintN) in { -      for(inti =0; I < n; i++) to     { +Par[i] =i; -High[i] =0; the     } * } $ //root of the query treePanax Notoginseng intFind (intx) - { the     returnPAR[X] = = x? X:PAR[X] = Find (par[x]);//Path Compression + } A voidUniteintXinty) the { +x =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 -     { thePar[y] =x; -         if(High[x] = = High[y]) high[x]++;Wuyi     } the } - BOOLSame (intXinty) Wu { -     returnFind (x) = =Find (y); About } $ intKruskal (intNintM//points N, number of sides M - { -     intSum_mst =0;//MST weight Value -     intnum=0;//The number of edges that have been selected ASort (A, A + M);//side to sort +Init (n);//initializing and checking the set the      for(inti =0; I < m; i++) -     { $         intU =a[i].u; the         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); -ans[tot++] =A[i]; inMaxx =Max (Maxx, A[I].W); thesum_mst++; thenum++; AboutUnite (U-1, V-1); the         } the         if(Num >= N-1) Break; the     } +     returnSum_mst; -     //printf ("Weight of MST is%d\n", sum_mst); the }Bayi intMain () the { the      while(Cin >> N >>m) -     { -tot = Maxx =0; the          for(inti =0; I < m; i++) the         { thescanf"%d%d%d", &a[i].u, &AMP;A[I].V, &A[I].W); the         } -         intsum =Kruskal (n, m); thecout<<maxx<<Endl; thecout<<sum<<Endl; the          for(inti =0; i < tot; i++)94cout<<ans[i].u<<" "<<ans[i].v<<Endl; the     } the     return 0; the}

POJ-1861 Network---minimum spanning tree

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.