Codevs 2627 Cun Tong

Source: Internet
Author: User

time limit: 1 sspace limit: 32000 KBtitle level: Golden GoldTitle 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 spend the least, 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).
In the second row, some rows are followed by another row. Of course, the diagonal will be 0 because there will be no line from the I-farm. End: The subsequent line contains a n*n matrix that represents the distance between each farm. In theory, they are n rows, each consisting of n spaces separated by a space, in effect, they are limited to 80 characters, so the field is to itself.

Output description Output Description

There is only one output, which contains the minimum length of fiber connected to each farm.

Sample input Sample Input

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 & Hint

No scope for the time being.

Portal

Kruskal
#include <algorithm>#include<iostream>#include<cstring>#include<cstdio>using namespacestd;structnode{intx, Y, Z;} e[101*101];intJ,i,n,tot;intfa[101];BOOLCMP (node A,node b) {returna.z<b.z;}intFindintk) {    if(fa[k]==k)returnK; Elsefind (Fa[k]);}intMain () {intA; CIN>>N;  for(i=1; i<=n;++i) fa[i]=i;  for(i=1; i<=n;++i) {         for(j=1; j<=n;++j) {cin>>A; if(a) {tot++; e[tot].x=i; E[tot].y=J; E[tot].z=A; }        }    }    intans=0, k=0; Sort (e+1, e+1+tot,cmp);  for(i=1; i<=tot;++i) {intFx=find (e[i].x), fy=find (E[I].Y); if(fx!=FY) {Fa[fy]=FX; Ans+=e[i].z; K++; }        if(k==n-1) Break; } cout<<ans;}

Codevs 2627 Cun Tong

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.