P1546 shortest network Agri-Net, p1546agri-net

Source: Internet
Author: User

P1546 shortest network Agri-Net, p1546agri-net
Background

John the farmer was elected as the mayor of his town! One of his campaign promises to build the Internet in town and connect to all farms. Of course, he needs your help.

Description

John has arranged a high-speed network line for his farm. He wants to share the line with other farms. To minimize consumption, he wants to build the shortest optical fiber to connect all farms.

You will get a list of connection fees between farms. You must find the solution that connects all farms and uses the shortest fiber optics. The distance between two farms is no more than 100000.

Input/Output Format Input Format:

Row 1: Number of farms, N (3 <= N <= 100 ).

The second line... ends: the subsequent rows contain a matrix of N * N, indicating the distance between each farm. Theoretically, they are N rows, each of which consists of N numbers separated by spaces. In fact, they are limited to 80 characters. Therefore, some rows are followed by other rows. Of course, the diagonal line will be 0, because there will be no line from the I farm to itself.

Output Format:

There is only one output that contains the minimum length of the optical fiber connecting to each farm.

Input and Output sample Input example #1:
40 4 9 214 0 8 179 8 0 1621 17 16 0
Output sample #1:
28
Description

The question translation is from NOCOW.

USACO Training Section 3.1

 

The raw kurskal, note that the fa array must be large enough!

 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 #include<algorithm> 6 using namespace std; 7 const int MAXN=4001; 8 void read(int & n) 9 {10     char c='+';int x=0;11     while(c<'0'||c>'9')c=getchar();12     while(c>='0'&&c<='9')13     x=x*10+(c-48),c=getchar();14     n=x;15 }16 struct node17 {18     int u,v,w,nxt;19 }edge[MAXN*5];20 int head[MAXN];21 int num=1;22 void add_edge(int x,int y,int z)23 {24     edge[num].u=x;25     edge[num].v=y;26     edge[num].w=z;27     edge[num].nxt=head[x];28     head[x]=num++;29 }30 int fa[MAXN*6];31 int n;32 inline int comp(const node &a ,const node & b)33 {34     return a.w<b.w;35 }36 inline int find(int x)37 {38     if(fa[x]!=x)39     return fa[x]=find(fa[x]);40     return fa[x];41 }42 inline void unionn(int x,int y)43 {44     int fx=find(x);45     int fy=find(y);46     fa[fx]=fy;47 }48 inline void kruskal()49 {50     int tot=0;51     int ans=0;52     sort(edge+1,edge+num,comp);53     for(int i=1;i<=num-1;i++)54     {55         int now=edge[i].u;int will=edge[i].v;56         if(find(now)!=find(will))57         {58             unionn(now,will);59             tot++;60             ans+=edge[i].w;61         }62         if(tot==n-1)63         break;64     }65     printf("%d",ans);66 }67 int main()68 {69     //freopen("agrinet.in","r",stdin);70     //freopen("agrinet.out","w",stdout);71     read(n);72     for(int i=0;i<=n;i++)73         fa[i]=i;74     for(int i=1;i<=n;i++)75         for(int j=1;j<=n;j++)76         {77             int x;78             read(x);79             if(x!=0)80             add_edge(i,j,x);81         }82     kruskal();83     return 0;84 }

 

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.