Title 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
//Bare Prim minimum spanning tree#include <cstdio>#include<iostream>#include<algorithm>#include<cstring>#defineINF 9999999#defineM 110using namespacestd;intMap[m][m],lowcost[m],vis[m],ans,n;intMain () {memset (map,0x3f3f3f3f,sizeof(map)); scanf ("%d",&N); for(intI=1; i<=n;i++) for(intj=1; j<=n;j++) { intx; scanf ("%d",&x); if(x) map[i][j]=x; } intpos=1; vis[1]=1; for(intI=1; i<=n;i++) Lowcost[i]=Map[pos][i]; for(intI=1; i<n;i++) { intminn=INF; for(intj=1; j<=n;j++) if(lowcost[j]<minn&&!Vis[j]) {Minn=Lowcost[j]; POS=J; } ans+=Minn; Vis[pos]=1; for(intj=1; j<=n;j++) if(!vis[j]&&lowcost[j]>Map[pos][j]) lowcost[j]=Map[pos][j]; } printf ("%d", ans); return 0;} View Code
Village Pass (Codevs 2627)