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
 /*题目大意:描述 :有个城市叫做H市。其中有很多个村庄,村庄之间通信基本靠吼,
交通基本靠走,很不方便。这个市长知道了这个情况,为了替市民着想,决定修建高铁。
每修建一米花费1美元。现在市长请了最著名的工程师来修建高铁,自然这个工程师会让修建高铁的费用最少。
不幸的是,在修建了高铁之后就病逝了。现在市长希望知道在修建完成的这些高铁路中最小生成树中最大的边的值。
他请你来帮助他,如果你计算正确,市长将会送你一辆兰博基尼。
输入:
第一行一个数T,表示接下来有多少组数据。
接下来每组测试数据的第一行有一个数N(3<=N<=500),表示村庄数目。然后是一个二维数组,
第i行第j列表示第i个村庄到第j个村庄的距离。 输出:
只有一个数,输出市长希望知道的已经修成的高铁中最小生成树中最大的边的值。*/
#include<cstdio>
#include<algorithm>
using namespace std;
int fa[];
struct stu
{
int from,to,al;
}st[];
bool cmp(stu a,stu b)
{
return a.al<b.al;
}
int find(int a)
{
int r=a;
while(r != fa[r])
{
r=fa[r];
}
return r;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int i,j,a,k,n,min=;
int num=-;
scanf("%d",&n);
for(i = ; i <= n ; i++)
{
fa[i]=i;
}
for(i = ; i <= n ; i++)
{
for(j = ; j <= n ; j++)
{
scanf("%d",&a);
if(i < j) //i到j的距离和j到i的距离一样,避免重复保存
{
num++;
st[num].from=i;
st[num].to=j;
st[num].al=a;
}
}
}
k=;
sort(st,st+num+,cmp); //按两地间的距离从小到大排序
for(i = ; i <= num ; i++)
{
if(k == n-) //n个节点需要n-1个边
{
break;
}
if(find(st[i].from) != find(st[i].to))
{
fa[find(st[i].from)] = find(st[i].to);
k++;
}
}
printf("%d\n",st[i-].al); //因为按两地距离最近的开始找,所以找到n-1个有效边的时,第n-1个边最大
}
}

POJ 2485 Highways (求最小生成树中最大的边)的更多相关文章

  1. POJ 2485 Highways( 最小生成树)

    题目链接 Description The islandnation of Flatopia is perfectly flat. Unfortunately, Flatopia has no publ ...

  2. POJ 2485 Highways【最小生成树最大权——简单模板】

    链接: http://poj.org/problem?id=2485 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...

  3. poj 2485 Highways (最小生成树)

    链接:poj 2485 题意:输入n个城镇相互之间的距离,输出将n个城镇连通费用最小的方案中修的最长的路的长度 这个也是最小生成树的题,仅仅只是要求的不是最小价值,而是最小生成树中的最大权值.仅仅须要 ...

  4. POJ 2485 Highways (prim最小生成树)

    对于终于生成的最小生成树中最长边所连接的两点来说 不存在更短的边使得该两点以不论什么方式联通 对于本题来说 最小生成树中的最长边的边长就是使整个图联通的最长边的边长 由此可知仅仅要对给出城市所抽象出的 ...

  5. poj 2485 Highways(最小生成树,基础,最大边权)

    题目 //听说听木看懂之后,数据很水,我看看能不能水过 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stri ...

  6. poj 2485 Highways

    题目连接 http://poj.org/problem?id=2485 Highways Description The island nation of Flatopia is perfectly ...

  7. POJ 1751 Highways 【最小生成树 Kruskal】

    Highways Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 23070   Accepted: 6760   Speci ...

  8. POJ2395 Out of Hay(求最小生成树中最大的边权,Kruskal)

    POJ2395 Out of Hay 寻找最小生成树中最大的边权. 使用 Kruskal 求解,即求选取的第 \(n-1\) 条合法边. 时间复杂度为 \(O(e\log e)\) . #includ ...

  9. poj 2485 求最小生成树中 最长的一条边

    Sample Input 1 //T 3 //n0 990 692 //邻接矩阵990 0 179692 179 0Sample Output 692 prim # include <iostr ...

随机推荐

  1. hdu1301 Jungle Roads 基础最小生成树

    #include<iostream> #include<algorithm> using namespace std; ; int n, m; ]; struct node { ...

  2. JMeter配置JDBC测试SQL Server/MySQL

    一.配置SQL Server 1.下载sql驱动,将sqljdbc4.jar放到JMeter安装目录/lib下. 2.启动JMeter,右键添加->配置文件->JDBC Connectio ...

  3. python之self本类对象

    不知道写点啥好,讲的都太绕了 似懂非懂,貌似懂了 以后再补充吧,视频day8_3中的20——60分钟那一部分

  4. Linux、Windows 下分割、合并rar文件

    1.分割rar 1.1 linux下分割压缩rar 安装rar和unrar和序 $sudo aptitude install rar unrar 示例,分割压缩temp文件,每个包为1MB $rar ...

  5. 对protected修饰符的范围用代码说明(同时说明用protected修饰的属性,在继承时,一定程度上破坏了封装)

    目录结构: 本类: 本包: 子孙类: 其他包:

  6. h5-23-百度地图api

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name ...

  7. Web service简介 与servletContext的参数

    Web service顾名思义是基于web的服务,它是一种跨平台,跨语言的服务. 我们可以这样理解它,比如说我们可以调用互联网上查询天气信息的web服务,把它嵌入到我们的B/S程序中,当用户从我们的网 ...

  8. P1554 梦中的统计

    题目背景 Bessie 处于半梦半醒的状态.过了一会儿,她意识到她在数数,不能入睡. 题目描述 Bessie的大脑反应灵敏,仿佛真实地看到了她数过的一个又一个数.她开始注意每一个数码(0..9):每一 ...

  9. JavaScript整理

    JavaScript是脚本语言 常用对话框: alert()——警告对话框,作用是弹出一个警告对话框 confirm()——带确定和取消按钮,返回True或false prompt()——弹出一个可以 ...

  10. 自動獲取外網IP并發郵件

    問題: 公司有一Web系統需開放給香港Office公司查詢資料,但最近動態域名需實名認證, 因系統較小型,非公開大範圍使用,所以再認證一域名也沒多大必要, 所以想定時生成一封能查詢外網IP的郵件發送給 ...