Rokua P1546 Shortest Network agri-net

Source: Internet
Author: User

Shortest network Agri-net "problem 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. The distance between two farms will not exceed 100000. "Input Format"
first line: td> number of farms, N ( 3<=n<= ).
second line : Knot Tail later lines contain a matrix of n*n , and represents the distance between each farm. Theory , they are n lines, each line consists of a number of spaces separated by N . In fact, they are restricted to , with characters, so some rows are followed by others. Of course, the diagonal line will be 0 because there will be no lines from i A farm to itself.
The output format has only one output, which contains the minimum length of the fiber connected to each farm. "Input Sample" Agrinet.in 4 0 4 9 21 4 0 8 17 9 8 0 16 21 17 16 0 "Output Example" Agrinet.out 28
#include <iostream>#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>using namespacestd;structtree{int  from, to; intQuan;}; Tree map[9901];intfather[101];intN,x,k,m,tot;intFindintl) {    if(father[l]!=l) father[l]=find (Father[l]); returnfather[l];}voidUnionn (intR1,intR2) {    intr3=find (R1); intr4=find (R2); if(r3!=R4) FATHER[R3]=R4;}BOOLcmpConstTree &a,ConstTree &b) {    if(A.quan < B.quan)return 1; return 0;}intMain () {Ios::sync_with_stdio (false); CIN>>N;  for(intI=1; i<=n;i++) Father[i]=i;  for(intI=1; i<=n; i++)    {         for(intj=1; j<=n; J + +) {cin>>x; if(x!=0) {m++; MAP[M]. from=i; Map[m].to=J; Map[m].quan=x; }}} sort (Map+1, map+m+1, CMP);  for(intI=1; i<=m;i++)    {        if(Find (Map[i). from)!=find (map[i].to)) {Unionn (map[i]. from, map[i].to); Tot+=Map[i].quan; K++; }        if(k = = n1) Break; } cout<<tot; return 0;}/*4 9 214 0 8 179 8 0 1621 0*/

Rokua P1546 Shortest Network agri-net

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.