POJ1258 Agri-Net
传送门: POJ1258
关键词: 图;Prim
Description
Farmer John has been elected mayor of his town! One of his campaign promises was to bring internet connectivity to all farms in the area. He needs your help, of course.
Farmer John ordered a high speed connection for his farm and is going to share his 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.
农夫John被选为他所在市的市长!而他的竞选承诺之一是将在该地区的所有的农场用互联网连接起来。当然,他现在需要你的帮助。John的农场已经连上高速连接的互联网,现在他要将这一连接与其他农场分享。为了减少成本,他希望用最短长度的光纤将他的农场与其它农场连接起来。
Given a list of how much fiber it takes to connect each 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 two farms will not exceed 100,000.
给出连接任意两个农场所需要的光纤长度的列表,请你找到将所有农场连接一起所需要光纤的最短长度。每个农场必须和其它的农场相连,以便数据包可以从任何一个农场流向其它任意农场。任何两个农场之间的距离不超过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 shows the distance from on farm to another. Logically, they are N lines of N space-separated integers. Physically, they are limited in length to 80 characters, so some lines continue onto others. Of course, the diagonal will be 0, since the distance from farm i to itself is not interesting for this problem.
输入包含若干组测试用例。每组测试用例的第一行给出农场数目N (3 <= N <= 100)。然后是N x N的邻接矩阵,其每个元素表示一个农场与另一个农场之间的距离。给出N行,每行有N个空格间隔的整数。一行接着一行地输入,每行不超过80个字符。当然,矩阵的对角线为0,因为每个农场到自己的距离为0。
Output
For each case, output a single integer length that is the sum of the minimum length of fiber required to connect the entire set of farms.
对于每个测试用例,输出一个整数,表示把所有农场连接在一起所需的光纤的最短距离。
Sample Input
4
0 4 9 21
4 0 8 17
9 8 0 16
21 17 16 0
Sample Output
28
Source
USACO 102
解析
- 将农场间的道路转化为带权无向连通图。
- 农场作为节点(John的农场为节点0),任意两个农场之间以边相连,两个农场之间的距离为相连边的权值。
- 因此,题目计算“将所有农场连接在一起所需要的光纤的最短长度”,就是求这个带权无向