Shortest network Agri-Net and network Agri-Net

Source: Internet
Author: User

Shortest network Agri-Net and network Agri-Net
Problem description: John, a 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. 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 format

The first line: Number of farms,N(3 <= N <= 100).
Row 2..EndTail The subsequent rows containN * NMatrix,The distance between each farm. TheoryThey areNLine, each line fromNWhich are separated by spaces. In fact, theyRestricted in80Therefore, some rows are followed by other rows. Of course, diagonalThe line will be0Because there will be no line fromIFarm to itself.
Output Format: only one output, including the minimum length of the optical fiber connecting to each farm. [Input example] agrinet. in 4 0 4 9 21 4 0 8 17 9 8 0 16 21 16 16 0 [Output example] agrinet. out 28
 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 6 int num=1; 7 struct node 8 { 9     int u;10     int v;11     int w;12     int next;13 }edge[10001];14 int head[10001];15 int f[10001];16 int tot=0;17 int cmp(const node &a,const node &b)18 {19     if(a.w<b.w)return 1;20     else return 0;21 }22 int find(int x)23 {24     if(f[x]!=x)25     f[x]=find(f[x]);26     return f[x];27 }28 void unionn(int x,int y)29 {30     int fx=find(x);31     int fy=find(y);32     if(fx!=fy)33     f[fx]=fy;34 }35 int main()36 {37     int n;38     scanf("%d",&n);39     for(int i=0;i<=n;i++)head[i]=-1;40     for(int i=0;i<=n;i++)f[i]=i;41     for(int i=1;i<=n;i++)42     {43         for(int j=1;j<=n;j++)44         {45             int kk;46             scanf("%d",&kk);47             if(kk!=0)48             {49                 edge[num].w=kk;50                 edge[num].u=i;51                 edge[num].v=j;52                 edge[num].next=head[i];53                 num++;54             }55         }56     }57     int k=0;58     sort(edge+1,edge+num,cmp);59     for(int i=1;i<=num-1;i++)60     {61         if(find(edge[i].u)!=find(edge[i].v))62         {63             unionn(edge[i].u,edge[i].v);64             tot=tot+edge[i].w;65             k++;66         }67         if(k==n-1)break;68     }69     printf("%d",tot);70     return 0;71 }

 

 

Related Article

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.