poj 2485 Highways

#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
#define N 1000
int bing[N];
struct coord
{
	int x, y;
	int quan;
};
bool cmp(const coord a, const coord b)
{
	return a.quan < b.quan;
}
int search(int a)
{
	if (a == bing[a])
		return a;
	else
		return bing[a] = search(bing[a]);
}
bool getin(int a, int b)
{
	int t1, t2;
	t1 = search(a);
	t2 = search(b);
	if (t1 != t2)
	{
		bing[t1] = t2;
		return true;
	}
	return false;
}
int main()
{
	int i;
	cin >> i;
	while (i--)
	{
		int a, b, ge, quan;
		vector<coord> map;
		int sb;
		coord ak_47;
		int count = 0;
		scanf("%d", &ge);
		for (a = 1; a <= ge; a++)
		{
			bing[a] = a;
			for (b = 1; b <= ge; b++)
			{
				scanf("%d", &quan);
				if (quan != 0 && a > b)
				{
					ak_47.quan = quan;
					ak_47.x = a;
					ak_47.y = b;
					map.push_back(ak_47);
				}
			}
		}
		sort(map.begin(), map.end(), cmp);
		for (int a = 0; a != map.size(); a++)
		{
			//cout << map[a].quan << endl;
			if (getin(map[a].x, map[a].y))
			{
				count++;
				if (count == ge - 1)
					sb = map[a].quan;
			}
		}
		printf("%d\n", sb);
		//system("pause");
	}
}

一道最小生成树的水题,很简单就能做出来。题意就是找出来最小生成树中的最大边,需要注意的是对边进行排序的时候注意不要排反了。

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