这道题显然是一道最小生成树的问题,参考算法导论中的Kruskal方法,先对路径长度进行排序,然后使用并查集(Disjoint Set Union)来判断节点是否连通,记录连接所有节点的最后一条路径的长度即为最大的长度了。

下面的并查集算法还可以通过设置rank数组记录节点的等级来进一步优化。总的来说还是一道简单题。

#include <cstdio>
#include <algorithm>
using namespace std; struct Edge{
int x, y;
int dis;
}; int pre[]; int find(int x)
{
int r = x;
while (pre[r] != r){
r = pre[r];
}
int i = x, j;
while (i != r){
j = pre[i];
pre[i] = r;
i = j;
}
return r;
} bool joint(int x, int y)
{
int xRoot = find(x),
yRoot = find(y);
if (xRoot == yRoot)
return false;
pre[xRoot] = yRoot;
return true;
} bool cmp(Edge e1, Edge e2)
{
return e1.dis < e2.dis;
} int main()
{
int t;
scanf("%d", &t);
while (t--){
int n;
Edge edge[];
scanf("%d", &n);
for (int i = ; i < n; i++){
pre[i] = i;
}
int cnt = ;
for (int i = ; i < n; i++){
for (int j = ; j < n; j++){
int dis;
scanf("%d", &dis);
if (i > j)continue;
edge[cnt].x = i;
edge[cnt].y = j;
edge[cnt++].dis = dis;
}
}
sort(edge, edge + cnt, cmp);
int max = -;
for (int i = ; i < cnt; i++){
if (joint(edge[i].x, edge[i].y)){
max = edge[i].dis > max ? edge[i].dis : max;
}
}
printf("%d\n", max);
}
return ;
}

最新文章

  1. Javascript理解this对象
  2. 移动端-js触摸事件
  3. canvas 3D运动球效果 多球
  4. jS-模式之简单的订阅者和发布者模式
  5. Android拼图游戏
  6. [转载] 关关采集不能生成html的问题
  7. Invalid encoding name &quot;UTF8&quot;. 报错 XML
  8. http://www.blogjava.net/nokiaguy/category/37087.html
  9. Mysql中Key与Index的区别
  10. #include &lt;limits.h&gt;
  11. SQL整理5
  12. 微信原生支付 Native扫码支付( V3.3.7 版本)
  13. ArrayList 冷门方法
  14. tar.gz和tar.bz2
  15. CodeForces - 589B(暴力)
  16. Apache beam中的便携式有状态大数据处理
  17. 第二个Sprint冲刺第 七天(燃尽图)
  18. JS实现弹出层效果
  19. 广播机制的CS模型实现
  20. MySQL查看用户权限的两种方法

热门文章

  1. NOIP2013 提高组 Day2
  2. 重构改善既有代码设计--重构手法01:Extract Method (提炼函数)
  3. ① 设计模式的艺术-07.适配器(Adapter)模式
  4. JQuery和Servlet来实现跨域请求
  5. JSTL标签库笔记
  6. 如何关闭sublime更新提示
  7. jquery.cookie.js——jquery的cookie插件
  8. 第三讲:ifconfig:最熟悉又陌生的命令行
  9. Coursera在线学习---第五节.Logistic Regression
  10. Linux端口占用