sicily 1090 Highways 最小生成树

         题目是求最长的边,在生成最小生成树时,用一个变量保存最大的值即可

 

//Prim算法

#include <iostream>

#include <memory.h>

#define INF 1000000

#define MAX 200

using namespace std;



int main()

{

	int arcs[MAX][MAX];

	bool isvisit[MAX];

	int min_weight[MAX];



	int N;

	int cases;



	cin >> cases;



	while (cases--)

	{

		cin >> N;

		memset(isvisit, false, sizeof(isvisit));



		for (int i = 0; i < N; i++)

			for (int j = 0; j < N; j++)

				cin >> arcs[i][j];



		for (int i = 0; i < N; i++)

			min_weight[i] = arcs[0][i];



		isvisit[0] = true;

		int _min;

		int hold, result = 0, max_weight = 0;

		for (int i = 1; i < N; i++)

		{

			_min = INF;

			

			for (int j = 0; j < N; j++)

			{

				if (!isvisit[j] && min_weight[j] < _min)

				{

					_min = min_weight[j];

					hold = j;



				}

			}	

			max_weight = max(max_weight, _min);

			result += _min;



			isvisit[hold] = true;



			for (int j = 0; j < N; j++)

				if (!isvisit[j] && arcs[hold][j] < min_weight[j])

					min_weight[j] = arcs[hold][j];

		}



		cout << max_weight << endl ;

		if (cases != 0)

			cout << endl;

	}



	return 0;

}

你可能感兴趣的:(最小生成树)