POJ2485 - Highways - Kruscal最小生成树

Highways
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 26829   Accepted: 12275

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.



Kruscal算法加排序,并查集路径压缩优化


#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <limits>
using namespace std;

int T, N, mapp[510][510];
int p[250010], u[250010], v[250010], w[250010], r[250010];

bool cmp(const int i, const int j) {
	return w[i] < w[j];
}

int find(int x) {
	return p[x] == x ? x : p[x] = find(p[x]);
}

int Kruskal() {
	int maxn = numeric_limits<int>::min();
	for (int i = 0; i < N * N; i++) p[i] = i;
	for (int i = 0; i < N * N; i++) r[i] = i;

	for (int i = 0, cou = 0; i < N; i++) {
		for (int j = 0; j < N; j++) {
			u[cou] = i; v[cou] = j; w[cou] = mapp[i][j];
			cou++;
		}
	}
	sort(r, r + N * N, cmp); //??
	for (int i = 0; i < N * N; i++) {
		int e = r[i];
		int x = find(u[e]), y = find(v[e]);
		if (x != y) {
			p[y] = x;
			maxn = max(maxn, w[e]);
		}
	}
	return maxn;
}

int main()
{
	scanf("%d", &T);
	while (T--) {
		scanf("%d", &N);
		for (int i = 0; i < N; i++) {
			for (int j = 0; j < N; j++) {
				scanf("%d", &mapp[i][j]);
			}
		}
		printf("%d\n", Kruskal());
	}
	return 0;
}


用vector只保留上三角矩阵或下三角矩阵

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <limits>
#include <vector>

using namespace std;

int T, N, p[510];

struct Edge {
	int u, v, w;
	Edge(int u, int v, int w) : u(u), v(v), w(w) {}
};

vector<Edge> edges;

int find(int x) {
	return p[x] == x ? x : p[x] = find(p[x]);
}

bool cmp(const Edge a, const Edge b) {
	return a.w < b.w;
}

int Kruskal() {
	int maxn = numeric_limits<int>::min();
	for (int i = 0; i < N; i++) p[i] = i;

	sort(edges.begin(), edges.end(), cmp);

	for (int i = 0; i < edges.size(); i++) {
		int x = find(edges[i].u), y = find(edges[i].v);
		if (x != y) {
			p[y] = x;
			maxn = max(maxn, edges.at(i).w);
		}
	}
	return maxn;
}

int main()
{
	scanf("%d", &T);
	while (T--) {
		edges.clear(); //注意初始化
		scanf("%d", &N);
		int w;
		for (int i = 0; i < N; i++) {
			for (int j = 0; j < N; j++) {
				scanf("%d", &w);
				if (i > j) {
					edges.push_back(Edge(i, j, w));
				}
			}
		}
		printf("%d\n", Kruskal());
	}
	return 0;
}





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