最小生成树,MST,Prim算法,poj2485

10 阅读2分钟

最小生成树prim算法,参考严蔚敏的《数据结构C语言版》和郑州大学的课件

Highways

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 30366 Accepted: 13815

Description

The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public highways. So the traffic is difficult in Flatopia. The Flatopian government is aware of this problem. They're planning to build some highways so that it will be possible to drive between any pair of towns without leaving the highway system. 

Flatopian towns are numbered from 1 to N. Each highway connects exactly two towns. All highways follow straight lines. All highways can be used in both directions. Highways can freely cross each other, but a driver can only switch between highways at a town that is located at the end of both highways. 

The Flatopian government wants to minimize the length of the longest highway to be built. However, they want to guarantee that every town is highway-reachable from every other town.

Input

The first line of input is an integer T, which tells how many test cases followed. 
The first line of each case is an integer N (3 <= N <= 500), which is the number of villages. Then come N lines, the i-th of which contains N integers, and the j-th of these N integers is the distance (the distance should be an integer within [1, 65536]) between village i and village j. There is an empty line after each test case.

Output

For each test case, you should output a line contains an integer, which is the length of the longest road to be built such that all the villages are connected, and this value is minimum.

Sample Input

1

3
0 990 692
990 0 179
692 179 0

Sample Output

692

Hint

Huge input,scanf is recommended.

Source

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <climits>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
using namespace std;
#define INF 0x7FFFFFFF
const int maxn = 500 + 7;
int prim[maxn][maxn]; //邻接矩阵
int MST(int matrix[][maxn], int n); //最小生成树Prim算法,时间复杂度O(N^2)
int main() {
	int T;
	scanf("%d", &T);
	while (T--) {
		int n;
		scanf("%d", &n);
		for (int i = 0; i < n; ++i)
			for (int j = 0; j < n; ++j)
				scanf("%d", &prim[i][j]);
		int longest = MST(prim, n);
		printf("%d\n", longest);
	}
	return 0;
}

int MST(int matrix[][maxn], int n) { //返回最短路径上的最大值
	int lowCost[maxn]; //
	int longest = INT_MIN; //若返回最短路径长度,修改longest相关语句
	for (int i = 0; i < n; ++i)
		lowCost[i] = matrix[0][i];
	for (int i = 1; i < n; ++i) {
		int minW = INF, u = 0;
		for (int j = 1; j < n; ++j) {
			if (minW > lowCost[j] && lowCost[j] != 0) {
				minW = lowCost[j];
				u = j; //记住该点
			}
		}
		lowCost[u] = 0; //by the wey, lowCost[v] = 0 表示点v在最短路径上
		longest = max(minW, longest);
		for (int j = 1; j < n; ++j) //更新lowCost,
			lowCost[j] = min(lowCost[j], matrix[u][j]);
	}
	return longest;
}
          iclosedge1(v2)2(v3)3(v4)4(v5)5(v6)UV-Uk
AdjvexlowcostV16V11V15{v1}{v2,v3,v4,v5,v6}2
AdjvexlowcostV350V15V36V34{v1,v3}{v2, v4, v5, v6}5
AdjvexlowcostV350V62V360{v1,v3,v6}{v2,v4, v5}3
AdjvexlowcostV3500V360{v1,v3,v6, v4}{v2, v5}1
Adjvexlowcost000V230{v1,v3,v6,v4,v2}{v5}4
Adjvexlowcost00000{v1,v3,v6,v4,v2,v5}{ }


author wjqin 2017-4-1 15:50:29

\