1231 Optimal cabling Problems

Source: Internet
Author: User

1231 Optimal cabling problems

time limit: 1 sspace limit: 128000 KBtitle level: Silver SolvingTitle Description Description

Schools need to connect n computers, and the cost of connection between the different 2 computers may be different. To save money, we consider the end of indirect data transfer, where one computer can indirectly connect to another computer via another computer.

In order for any two computers to be connected (whether directly or indirectly), you need to use a network cable between several computers directly connected, now want to make the total cost of connection is the most province, let you program to calculate this minimum cost.

Enter a description Input Description

Enter the first behavior two integer n,m (2<=n<=100000,2<=m<=100000), which represents the total number of computers, and the number of connections that can be connected to each other. Next m line, three integers per line a,b,c means that the charge for establishing a connection between machine A and machine B is c. (the topic guarantees that there must be a viable connectivity scheme, the data may have a different weight, but no self-loop)

Output description Output Description

The output is an integer that represents the total connection cost for the most province.

Sample input Sample Input

3 3

1 2 1

1 3 2

2 3 1

Sample output Sample Output

2

Data range and Tips Data Size & Hint

The final answer needs to be saved with a long long type.

Category labels Tags Click here to expandThe minimum spanning tree graph theory and the tree structure of the search set Code:
#include <cstdio>#include<iostream>#include<cstring>#include<cstdlib>#include<algorithm>using namespacestd;intN,m,k,x,tx,ty,len;//Kruskal AlgorithmLong Longtot=0;Const intmaxn=1000100;intFA[MAXN],V[MAXN],C[MAXN];structnode{intX,y,v;} A[MAXN];intFindintx) {    if(fa[x]!=x) fa[x]=find (fa[x]); returnfa[x];}voidUnionn (intXinty) {    intrx=find (x); intry=find (y); if(rx!=ry) Fa[rx]=ry;//If two points are not within a tree, then x this tree's heel node is attached to the root node of the tree y .}intcmpConstNode &q,ConstNode &h) {    returnq.v<h.v;}intMain () {scanf ("%d%d",&n,&m);  for(intI=1; i<=m;i++) {scanf ("%d%d%d",&tx,&ty,&Len); a[i].x=tx,a[i].y=ty,a[i].v=Len; }     for(intI=1; i<=n;i++) fa[i]=i; Sort (a+1, a+m+1, CMP);  for(intI=1; i<=m;i++){        if(Find (a[i].x)! =find (A[I].Y))            {Unionn (A[I].X,A[I].Y); Tot+=a[i].v; K++; }        if(k==n-1) Break; } printf ("%lld\n", tot); return 0;}

1231 Optimal cabling Problems

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.