POJ 2485 Highways

#include <stdio.h>
#define MAX_VILLAGES 500
#define INF 70000

int distanceMatrix[MAX_VILLAGES + 1][MAX_VILLAGES + 1];
int distanceArray[MAX_VILLAGES + 1];
int visited[MAX_VILLAGES + 1];

int main(){

	int numOfTestcases;
	scanf("%d", &numOfTestcases);
	int testcase;
	for (testcase = 1; testcase <= numOfTestcases; testcase++){
		int numOfVillages;
		scanf("%d", &numOfVillages);
		int from, to;
		for (from = 1; from <= numOfVillages; from++)
			for (to = 1; to <= numOfVillages; to++)
				scanf("%d", &distanceMatrix[from][to]);
		
		int village;
		for (village = 1; village <= numOfVillages; village++){
			visited[village] = 0;
			distanceArray[village] = distanceMatrix[1][village];
		}
		visited[1] = 1;

		int result = 0;
		while (1){
			int nearestVillage = 0;
			int minDistance = INF;
			for (village = 2; village <= numOfVillages; village++)
				if (visited[village] == 0 && distanceArray[village] < minDistance){
					nearestVillage = village;
					minDistance = distanceArray[village];
				}

			if (nearestVillage == 0)
				break;

			if (minDistance > result)
				result = minDistance;

			visited[nearestVillage]  = 1;

			for (village = 2; village <= numOfVillages; village++)
				if (visited[village] == 0 && distanceMatrix[village][nearestVillage] < distanceArray[village])
					distanceArray[village] = distanceMatrix[village][nearestVillage];
		
		}
		printf("%d\n", result);
	}

	return 0;
}

你可能感兴趣的:(最小生成树,poj,Prim,Highways,2485)