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

题意: 先输入T,有T组数据。再输入有N个点。N行N列 每一个数据代表I点到J点的距离。求完毕一个 最小生成树种 的  最大边。


#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
#define q 505
int fa[q];
int map[q][q];
struct node{
int x,y,l;
}q[q*q/2];
int find(int x)
{
return x==fa[x]? x:find(fa[x]);
}
int cmp(node a,node b)
{
return a.l<b.l;
}
int main()
{
int k,t,n,i,j,max1;
scanf("%d",&t);
while(t--)
{
max1=0;
k=0;
scanf("%d",&n);
for(i=1;i<=n;i++)
{
fa[i]=i;
for(j=1;j<=n;j++)
{
scanf("%d",&map[i][j]);
if(i<j) // 优化:去掉重边还有中间的0
{
k++; //不知道有多少边。这样统计
q[k].x=i;
q[k].y=j;
q[k].l=map[i][j];
}
}
}
sort(q+1,q+1+k,cmp); //依据每条边的距离排序
for(i=1;i<=k;i++)
{
if(fa[find(q[i].x)]!=fa[find(q[i].y)]) //并查集思想。。 {
fa[find(q[i].x)]=find(q[i].y);
if(q[i].l>max1)
max1=q[i].l;
}
}
cout<<max1<<endl;
}
return 0;
}

Kruskal 模板。。

POJ 2485 Highways 最小生成树 (Kruskal)的更多相关文章

  1. POJ 2485 Highways(最小生成树+ 输出该最小生成树里的最长的边权)

                                                                                                         ...

  2. poj 2485 Highways 最小生成树

    点击打开链接 Highways Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19004   Accepted: 8815 ...

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

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

  4. poj 2485 Highways

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

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

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

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

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

  7. POJ 2485 Highways (求最小生成树中最大的边)

    Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public h ...

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

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

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

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

随机推荐

  1. 【IIS7.5】Asp文件上传限制,加载页面大小限制

    运行环境 window server 2008 R2 X64.IIS7.5.应用程序池.Net4.0 经典模式 分析 IIS7.5默认有两处上传限制: 第一处在,选择左侧的站点,然后找到后侧的管理—— ...

  2. LN : leetcode 538 Convert BST to Greater Tree

    lc 538 Convert BST to Greater Tree 538 Convert BST to Greater Tree Given a Binary Search Tree (BST), ...

  3. [ NOI 2001 ] 方程的解数

    \(\\\) \(Description\) 已知一个 \(N\) 元高次方程: \[ k_1x_1^{p_1}+k_2x_2^{p_2}+...+k_nx_n^{p_n}=0 \] 要求所有的 \( ...

  4. 清除浮动(float)的影响

    浮动会导致父元素塌陷如图: 解决办法: 父元素overflow:hidden,如图 末尾插入子元素clear,如图 为甚么,父元素overflow:hidden会解决塌陷问题? 来自知乎貘吃馍香的回答 ...

  5. R语言曲线拟合函数(绘图)

    曲线拟合:(线性回归方法:lm) 1.x排序 2.求线性回归方程并赋予一个新变量     z=lm(y~x+I(x^2)+...) 3.plot(x,y)    #做y对x的散点图 4.lines(x ...

  6. Windows离线安装Python第三方库的方法

    在window中,离线安装第三方模块, 1.下载第三方库的压缩文件,解压,将解压后的文件放到Python安装目录下的Lib\site_packages中 2. 将Python添加到环境变量里 3.进入 ...

  7. Knockout 实例

    定义见:http://baike.baidu.com/item/Knockout/18611883#viewPageContent 此处仅列举一个小例子 <p> <select da ...

  8. break和continue在多重循环中使用

    break和continue在多重循环中使用 关于break和continue在java中,break的作用是跳出循环,continue的作用是跳出本次循环. 我们一般情况下,这样使用: public ...

  9. jenkins部署遇到离线问题如何解决

    部署jenkins页面时遇到离线问题如何解决 部署jenkins遇到一个问题,然后告诉我你的jenkins已经离线,什么鬼,后来找了很多博客 后来自己终于验证成功了,也分享给大家,只需把https改为 ...

  10. 3D赛瓦号——整装待发!

    随着岁末将至,twaver开发团队依旧马不停蹄,3d产品功能持续更新,新特效和功能目不暇接.现在,我们就利用一些新功能,制作一个全新“赛瓦号”飞船,大家看一下仿真程度是否有质的不同? 网页3d技术正在 ...