题目

Language:Default
Highways
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 36414 Accepted: 16290

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

13
0 990 692
990 0 179
692 179 0

Sample Output

692

Hint

Huge input,scanf is recommended.

Source

POJ Contest,Author:Mathematica@ZSU

分析
题目大概意思就是,输入城镇数量n,然后接下来是一个n×n的邻接矩阵,值就代表两点间的距离,输出最小生成树的最大权值。
知道这点就好办啦,构建一个结构体,把每组起点、终点和两点间的距离都存进去,然后用Kruskal算法求出最小生成树,把每一条边的权值都存入到一个新的数组中,然后排序输出最大值即可。

注意
1、正常输出即可,不要特意去增加换行符
2、由于输入的权值可能会非常大,所以用cin会超时,我就在这奉献了一次TLE(Time Limit Exceeded)。
cin和scanf的区别是scanf是格式化输入,printf是格式化输出,效率较高; cin是输入流,cout是输出流,效率稍低。cin与cout之所以效率低,因为是先把要输入/出的东西存入缓冲区,再输入/出,导致效率降低。 详情可参见大佬的一篇博客:https://www.cnblogs.com/limera/p/5405705.html

最后上代码

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
const int MAXN=1e5 + 10;struct node
{int u,v;//u:起点,v:终点 int dis;//两点距离
}N[MAXN];int pre[MAXN];
int n;
int find(int x)
{if(x==pre[x]) return x;return pre[x]=find(pre[x]);
}
bool cmp1(node a,node b)
{return a.dis < b.dis;
}
bool cmp2(int a,int b)
{return a>b;
}
void init()
{for(int i=0;i<=n;i++){pre[i]=i;}
}int main()
{int t,i,k,j;int a[MAXN];cin>>t;while(t--){memset(N,0,sizeof(N));memset(a,0,sizeof(a));k=0;cin>>n;init();for(i=0;i<n;i++)//将每条路径的起点、终点和距离都存入到结构体中 {for(j=0;j<n;j++){N[k].u=i;//起点 N[k].v=j;//终点 //cin>>N[k].dis;//如果输入数据过大,这样会超时 scanf("%d",&N[k].dis);//两点距离 k++;}}sort(N,N+k,cmp1);j=0;for(i=0;i<k;i++){int dx=find(N[i].u);int dy=find(N[i].v);if(dy!=dx){a[j++]=N[i].dis;pre[dy]=dx;}if(j==n-1) break;//最小生成树的条件:边数=顶点数-1 }sort(a,a+j,cmp2);cout<<a[0]<<endl;}return 0;
}

POJ 2485 - Highways(求最小生成树的最大权值-Kruskal算法)相关推荐

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

    Highways   大意:给你一个用邻接矩阵形式存储的有n个顶点的无向图,让你求它的最小生成树并求出在这个生成树里面最大的边的权值. 思路:用Prim求,判断条件改一下就行. PS:dis数组初始化 ...

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

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

  3. 求的带权图最小生成树的Prim算法和Kruskal算法

    求的带权图最小生成树的Prim算法和Kruskal算法 最小生成树的概念 最小生成树其实是最小权重生成树的简称. 一个连通图可能有多个生成树.当图中的边具有权值时,总会有一个生成树的边的权值之和小于或 ...

  4. 【数据结构】 最小生成树(四)——利用kruskal算法搞定例题×3+变形+一道大水题...

    在这一专辑(最小生成树)中的上一期讲到了prim算法,但是prim算法比较难懂,为了避免看不懂,就先用kruskal算法写题吧,下面将会将三道例题,加一道变形,以及一道大水题,水到不用高级数据结构,建 ...

  5. 最小生成树的prim算法和kruskal算法

    <span style="font-size:18px;">#include "stdafx.h" #include <iostream> ...

  6. 【最小生成树】Prim算法和Kruskal算法的区别对比

    Prim算法和Kruskal算法都是从连通图中找出最小生成树的经典算法- 从策略上来说,Prim算法是直接查找,多次寻找邻边的权重最小值,而Kruskal是需要先对权重排序后查找的- 所以说,Krus ...

  7. 数据结构与算法-最小生成树之克鲁斯卡尔(Kruskal)算法

    1. 算法步骤 Kruskal 算法可以称为"加边法",初始最小生成树边数为0,每迭代一次就选择一条满足条件的最小代价边,加入到最小生成树的边集合里. 1. 把图中的所有边按代价从 ...

  8. 加权无向图与最小生成树(Prim算法和Kruskal算法)

    目录 0 引入 1 图的最小生成树定义及相关约定 2 最小生成树原理 2.1 性质 2.2 切分定理 3 贪心思想 4 Prim算法 4.1 算法步骤 4.2 API设计 4.3 Java代码演示 5 ...

  9. 最小生成树之Prim算法和Kruskal算法

    一个连通图可能有多棵生成树,而最小生成树是一副连通加权无向图中一颗权值最小的生成树,它可以根据Prim算法和Kruskal算法得出,这两个算法分别从点和边的角度来解决. Prim算法 输入:一个加权连 ...

最新文章

  1. 2021第十二届蓝桥杯国赛总结-java大学c组
  2. 安装Exchange Server2016管理工具
  3. java http 401_服务器返回HTTP响应代码:401,URL:https
  4. IT精英们!一路走好!
  5. hihocoder #1333 : 平衡树·Splay2
  6. permute是最好的Mac上面的格式转换器
  7. Python__random库基本介绍
  8. 单元测试的编写(asp.net) (VS2017)
  9. 不联网的情况下,使用 electron-builder 快速打包全平台应用
  10. 机器面试-处理分类问题
  11. xp sp3不让dword shoot
  12. Java实现网上书店管理系统(idea+MySQL+navicat)
  13. PaddleOCR手写体训练摸索
  14. python椭圆曲线加密信息_ECC椭圆曲线加密算法:ECDH 和 ECDSA
  15. 【PTC Thingworx(三)】连接和监控工厂设备
  16. SuperPoint学习训练纪录 无训练版与带训练版本(一)
  17. 国内计算机博士去百度云,于博士Cadence视频教程60集全套百度网盘分享
  18. 泛函分析的几个空间和平行四边形法则
  19. C和C++的区别(2) 关键字
  20. 详解pandas中的groupy机制

热门文章

  1. python wordcloud 错误 ModuleNotFoundError: No module named 'query_integral_image'
  2. [jQuery]点击某元素之外触发事件
  3. Effective java -- 2 对于所有对象都通用到方法
  4. Mysql常用命令详解
  5. HDU2102 A计划
  6. You have new mail
  7. Android近场通信---NFC基础(一)(转)
  8. [痛并快乐着 国外开发者总结欧美游戏坑钱指南] 讀後感想
  9. 基于用户评价的评分模型
  10. 自然语言理解难在哪儿?