Codevs 1078 minimum Spanning tree Kruskal

Source: Internet
Author: User

Title Description Description

Farmer John was chosen to be the mayor of their town! One of his campaign promises was to set up the Internet in the town and connect to all the farms. Of course, he needs your help. John has arranged a high-speed Internet connection for his farm, and he wants to share the line with other farms. In order to make the least cost, he wanted to lay the shortest fiber to connect all the farms. You'll get a list of connections between farms, and you'll have to find a solution that connects all farms and uses the shortest possible fiber. No more than 100000 of the distance between farms per two

Enter a description Input Description

First line: Number of farms, N (3<=n<=100).

Second line: End: The next line contains a n*n matrix that represents the distance between each farm. In theory, they are n rows, each row consists of n spaces separated by a space, and in fact, each row is limited to 80 characters, so some rows are followed by others. Of course, the diagonal will be 0 because the distance from the I farm to its own is meaningless in the subject.

Output description Output Description

Only one output, is the minimum length of fiber connected to each farm and.

Sample input to sample

4

0 4 9 21

4 0 8 17

9 8 0 16

21 17 16 0

Sample output Sample Output

28

Data range and Tips Data Size & HintTest instructions: Minimum spanning tree: kruscal template topic Note WA Point
1#include <iostream>2#include <cstring>3#include <cstdio>4#include <queue>5#include <algorithm>6#include <stack>7 using namespacestd;8 structnode9 {Ten     intl; One     intR; A     intv; -}n[11000]; - intfa[101]; the intN; - intJishu; - intEXM; - BOOLcmpstructNode AA,structnode BB) + { -     if(AA.V&LT;BB.V)//WA point!!!  +      return true; A      return false; at } - voidInit () - { -      for(intI=1; i<n;i++) -fa[i]=i; - } in intFindintroot) - { to     if(root!=Fa[root]) +       returnfa[root]=find (Fa[root]); -     Else the       returnFa[root]; * } $ voidUnio (intAintb)Panax Notoginseng { -     intAa=find (a); the     intdb{find (b); +     if(aa!=BB) Afa[aa]=BB; the } + voidkruscal () - { $     intans=0; $      for(intI=0; i<jishu;i++) -     { -       intq=find (N[I].L); the       intw=find (N[I].R); -       if(q!=W)Wuyi       { then--; - Unio (q,w); Wuans+=n[i].v; -       } About       if(n==1) $        Break; -     } -cout<<ans<<Endl; - } A intMain () + { the      while(SCANF ("%d", &n)! =EOF) -     { $ init (); thejishu=0; the          for(intI=1; i<=n;i++) the           for(intj=1; j<=n;j++) the          { -scanf"%d",&EXM); inN[jishu].l=i; theN[jishu].r=J; then[jishu++].v=EXM; About          } theSort (n,n+jishu,cmp); the kruscal (); the     }  +     return 0; -      the}

Codevs 1078 minimum Spanning tree Kruskal

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.