1090. Highways
Constraints
Time Limit: 1 secs, Memory Limit: 32 MB
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 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.
Output
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.
This problem contains multiple test cases!
The first line of a multiple input is an integer T, then a blank line followed by T input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.
The output format consists of T output blocks. There is a blank line between output blocks.
Sample Input
1 3 0 990 692 990 0 179 692 179 0
Sample Output
692
这是一题最小生成树的应用。实现以下最小生成树就可以了
// Problem#: 1090
// Submission#: 2476289
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include <iostream>
#include <memory>
#include <algorithm>
#include <cstring>
using namespace std;
int root[501];
struct Edge{
int x;
int y;
int weight;
};
bool cmp(Edge a,Edge b) {
return a.weight < b.weight;
}
Edge edges[125020];
int main() {
int T;
cin >> T;
while(T --) {
// memset(root,0,sizeof(root));
int n;
cin >> n;
for(int i = 0;i < n;i ++) {
root[i] = i;
}
int a[500];
int edgelength = 0;
for(int i = 0;i < n;i ++) {
for(int j = 0;j < n;j ++) {
cin >> a[j];
}
for(int j = i + 1;j < n;j ++) {
edges[edgelength].x = i;
edges[edgelength].y = j;
edges[edgelength].weight = a[j];
edgelength ++;
}
}
//cout << edgelength<<endl;
sort(edges,edges + edgelength,cmp);
int result = 0;
for(int i = 0;i < edgelength ;i ++) {
if(root[edges[i].x] != root[edges[i].y]) {
//result += edges[i].weight;
if(result < edges[i].weight) {
result = edges[i].weight;
}
int aroot = root[edges[i].x];
int broot = root[edges[i].y];
for(int j = 0;j < n;j ++) {
if(root[j] == broot) {
root[j] = aroot;
}
}
}
}
cout << result <<endl;
if ( T != 0 )
cout << endl;
}
//system("pause");
return 0;
}