Agri-net-poj 1258 (Prim algorithm)

Source: Internet
Author: User

Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 44373 Accepted: 18127

Description

Farmer John had been elected mayor of his town! One of his campaign promises is to bring internet connectivity to all farms in the area. He needs your help, of course.
Farmer John ordered a high speed connection for he farm and is going to share he connectivity with the other farmers. To minimize cost, he wants to lay the minimum amount of optical fiber to connect his farm to all the other farms.
Given a list of how much fiber it takes to connect all pair of farms, you must find the minimum amount of fiber needed to Connect them all together. Each farm must connect to some other farm such that a packet can flow from any one farm to any other farm.
The distance between any and farms would not exceed 100,000.

Input

The input includes several cases. For each case, the first line contains the number of farms, n (3 <= n <= 100). The following lines contain the n x n conectivity matrix, where each element is shows the distance from on farm to another. Logically, they is n lines of n space-separated integers. Physically, they is limited in the length to the characters, so some lines continue onto others. Of course, the diagonal would be 0, since the distance from farm I to itself are not interesting for this problem.

Output

For each case, output a single integer length this is the sum of the minimum length of fiber required to connect the Entir e set of farms.

Sample Input

40 4 9 214 0 8 179 8 0 1621 17 16 0

Sample Output

28
1#include <stdio.h>2 #defineINF 1000003 intN;4 intmap[ $][ $];5 intdis[ $],vis[ $];6 intPrim () {7      for(intI=0; i<n;i++){8vis[i]=0;9dis[i]=INF;Ten     } Onedis[0]=0; A  -      for(intI=0; i<n;i++){ -         intmin=INF; the         intp; -          for(intj=0; j<n;j++){ -             if(!vis[j]&&min>Dis[j]) { -min=Dis[j]; +p=J; -             } +         } Avis[p]=1; at          for(intj=0; j<n;j++){ -             if(!vis[j]&&dis[j]>Map[p][j]) { -dis[j]=Map[p][j]; -             } -         } -     } in      for(intI=1; i<n;i++){ -dis[0]+=Dis[i]; to     } +     returndis[0]; - } the intMain () { *      while(~SCANF ("%d",&N)) { $      for(intI=0; i<n;i++){Panax Notoginseng          for(intj=0; j<n;j++){ -scanf"%d",&map[i][j]); the         } +     } A     intmin=Prim (); theprintf"%d\n", min); +     } -     return 0; $}

Agri-net-poj 1258 (Prim algorithm)

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.