Minimum spanning Tree two · Kruscal algorithm

Source: Internet
Author: User
Tags knowledge base

Time limit: 10000ms single point time limit: 1000ms memory limit: 256MB description

With the increase in the number of cities with small hi, the prim algorithm used in between has been unable to continue to be used-but fortunately, after a computer analysis, Little Hi has screened out some of the more suitable routes for building roads, and this number is not particularly large.

So the question becomes--Little hi now has n cities on hand, and the cost of building roads among some of these cities, little hi wants to know that the minimum cost will allow any two cities to reach each other through the roads they have built (assuming a, B, c three cities, Only the road between AB and BC can be built, and the two roads will be connected by AC.

Tip: The benefit of accumulating is that you can always extract what you want from your knowledge base! Input

Each test point (input file) has and has only one set of test data.

In a set of test data:

The 1th act of 2 integers n, M, indicates the number of cities owned by small hi and small hi filters out the number of routes.

The next M-line, each line describes a route, where I acts 3 integers n1_i, n2_i, V_i, respectively, representing the two endpoints of the route and the cost of building roads on this route.

For 100% of data, meet N<=10^5, m<=10^6, at any I meet 1<=n1_i, N2_i<=n, N1_i≠n2_i, 1<=v_i<=10^3.

For 100% of the data, to meet a certain existence of a scheme, so that any two cities can reach each other.

Output

For each set of test data, Output 1 integer ans, indicating that in order for any two cities to reach each other at least the required construction costs through the roads they build.

Sample input
5 291 2 6742 3 2493 4 6724 5 9331 2 7883 4 1472 4 5043 4 381 3 653 5 61 5 8651 3 5901 4 6822 4 2272 4 6361 4 3121 3 1432 5 1582 3 5163 5 1021 5 6051 4 994 5 2242 4 1983 5 8941 5 8453 4 72 4 141 4 185
Sample output
92


1#include"iostream"2#include"algorithm"3#include"Memory.h"4 using namespacestd;5 #defineMAXN 20000106 7 structNode {8     intx,y,l;9 }P[MAXN];Ten intFA[MAXN]; One intn,m; A intre; -  - BOOLCMP (Node A, Node B) the { -     returnA.L <B.L; - } -  +  - intFindset (intx) + { A     if(Fa[x] <0)returnx; at     returnFA[X] =Findset (fa[x]); - } -  - intMain () - { -CIN >> N >>m; in      for(inti =0; I <= N; ++i) -Fa[i] =-1; to      for(inti =1; I <= m; ++i) { +CIN >> p[i].x >> p[i].y >>P[I].L; -     } theSort (p,p+m,cmp); *     intRe =0; $      for(inti =1; I <= m; ++i) {Panax Notoginseng         intx =Findset (p[i].x); -         inty =Findset (P[I].Y); the         if(X! =y) { +Re + =P[I].L; AFA[X] + =Fa[y]; theFa[y] =x; +         } -     } $cout << re <<Endl; $System"Pause"); -     return 0; -}
code June

Minimum spanning Tree two · Kruscal algorithm

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.