题干:

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.

题目大意:

A国没有高速公路,因此A国的交通很困难。政府意识到了这个问题并且计划建造一些高速公路,以至于可以在不离开高速公路的情况下在任意两座城镇之间行驶。

A国的城镇编号为1到N, 每条高速公路连接这两个城镇,所有高速公路都可以在两个方向上使用。高速公路可以自由的相互交叉。

A国政府希望尽量减少最长高速公路的建设时间(使建设的最长的高速公路最短),但是他们要保证每个城镇都可以通过高速公路到达任意一座城镇。

解题报告:

介于有的题目确实Prim算法效率极高(对于n=2000这样的完全图,用Kruskal就作死了),我决定顺道学习一下Prim、、、(

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
const int MAX = 2e5 + 5;
const int INF = 0x3f3f3f3f;
int n;
int cost[666][666];
int dis[666];
bool vis[666];
int prim() {//这种写法适用于已知点是1~N的。int res = 0;for(int i = 0; i<=n; i++) dis[i] = INF, vis[i] = 0;dis[1] = 0;//假设从1开始while(1) {int v = 0;//这种写法的话上面初始化必须从0开始了。。for(int i = 1; i<=n; i++) {if(!vis[i] && dis[i] < dis[v]) v = i;}if(!v) break;//这种写法的话上面初始化必须从0开始了。。 vis[v] = 1;res = max(res,dis[v]) ;for(int i = 1; i<=n; i++) {if(!vis[i] && cost[v][i] < dis[i]) dis[i] = cost[v][i];//!vis[i]这一句加不加都一样的。}} return res;
}
int main()
{int t;cin>>t;while(t--) {scanf("%d",&n);for(int i = 1; i<=n; i++) {for(int j = 1; j<=n; j++) {scanf("%d",&cost[i][j]);}    }int ans = prim();printf("%d\n",ans);}return 0 ;}

Prim的另一种写法:(相对常用一些)

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
const int MAX = 2e5 + 5;
const int INF = 0x3f3f3f3f;
int n;
int cost[666][666];
int dis[666];
bool vis[666];
int prim() {//这种写法不一定必须点是1~N的,也可以是0~N-1的,只需要把下面第三行那个改成dis[0]=0就行了。int res = 0;for(int i = 1; i<=n; i++) dis[i] = INF, vis[i] = 0;//这种写法的话初始化就可以从1开始 dis[1] = 0;//假设从1开始while(1) {int v,minw = INF;for(int i = 1; i<=n; i++) {if(!vis[i] && dis[i] < minw) v = i,minw = dis[i];}if(minw == INF) break;vis[v] = 1;res = max(res,dis[v]) ;for(int i = 1; i<=n; i++) {if(!vis[i] && cost[v][i] < dis[i]) dis[i] = cost[v][i];}} return res;
}
int main()
{int t;cin>>t;while(t--) {scanf("%d",&n);for(int i = 1; i<=n; i++) {for(int j = 1; j<=n; j++) {scanf("%d",&cost[i][j]);}    }int ans = prim();printf("%d\n",ans);}return 0 ;}

【POJ - 2485 】Highways (最小生成树,Prim算法,瓶颈生成树)相关推荐

  1. [Java学习] 最小生成树——Prim算法

    文章目录 最小生成树 Prim算法流程 应用实例 求最小生成树 最小生成树 百度百科上对于最小生成树的定义是这样的:一个有 n 个结点的连通图的生成树是原图的极小连通子图,且包含原图中的所有 n 个结 ...

  2. D-OJ刷题日记:使用邻接矩阵实现最小生成树Prim算法 题目编号:1135

    理解: [理解prim算法本质--让一棵小树逐渐长大] Prim算法:又称为加边法,即每次选择最小权值的边加入到生成树中,然后再更新权值,如此反复,保证每次最优来达到最优解. Prim算法生成树用的是 ...

  3. 西南科技大学OJ题 求最小生成树(Prim算法)1075

    求最小生成树(Prim算法) 1000(ms) 10000(kb) 2256 / 4495 Tags: 生成树 求出给定无向带权图的最小生成树.图的定点为字符型,权值为不超过100的整形.在提示中已经 ...

  4. 最小生成树-Prim算法详解(含全部代码)

    目录 适用条件 测试所用图 算法详解 Prim算法代码 全部代码 实验结果 适用条件 加权连通图 测试所用图 所用原图及生成过程 其中,(a) 为原图,圆圈里面是节点的名称,边上的数字是边的权值.由实 ...

  5. 最小生成树Prim算法Java版

    最小生成树Prim算法Java版 算法描述: 在一个加权连通图中,顶点集合V,边集合为E 任意选出一个点作为初始顶点,标记为visit,计算所有与之相连接的点的距离,选择距离最短的,标记visit. ...

  6. 最小生成树 - Prim算法

    最小生成树 - Prim算法 思路: 采用 贪心策略,每次选取连通块外延的最短边和对应的点放入连通块,再更新新的连通块外延的边.连通部分逐渐扩大,最后将整个图连通起来,并且边长之和最小. 时间复杂度: ...

  7. * poj 1251 JungleRoad 最小生成树 Kruskal算法、Prim算法

    文章目录 Kruskal算法 模板:https://blog.csdn.net/Rain722/article/details/65642992 Prim算法 模板: poj 1251 JungleR ...

  8. 生成树的概念,最小生成树Prim算法 Kruskal算法

    求解最小生成树可以用Prim算法 Kruskal算法

  9. 最小生成树Prim算法

    MST性质理解 设图的总结点为V,以落在生成树上的结点集为U,那么尚未落在生成树上的结点集为V - U. 最小生成树最小边权选择:从U到V-U的连通边选择最短的边. prim算法思路 选择:MST性质 ...

  10. 【数据结构】最小生成树(Prim算法,普里姆算法,普利姆)、最短路径(Dijkstra算法,迪杰斯特拉算法,单源最短路径)

    文章目录 前置问题 问题解答 一.基础概念:最小生成树的定义和性质 (1)最小生成树(Minimal Spanning Tree)的定义 (2)最小生成树(MST)的性质 二.如何利用MST性质寻找最 ...

最新文章

  1. 架构设计的本质:系统与子系统、模块与组件、框架与架构
  2. windows下部署redis
  3. pip install python -32_pip安装python模块方法
  4. java两个数之间质数求法,求任意两个整数之间质数的个数的代码!!!(新手编写)...
  5. 在Java中衡量执行时间– Spring StopWatch示例
  6. 提高redis cluster集群的安全性,增加密码验证
  7. 【java笔记】缓冲流
  8. Let’s Encrypt 免费ssl加密
  9. RocketMQ的底层通信模块remoting 源码解析
  10. matlab电磁场与微波技术仿真pdf,MATLAB电磁场与微波技术仿真
  11. 微信公众平台注册流程
  12. 数据分析师面试常见的77个问题
  13. 【ENSP模拟器】RIP(HCNP)——RIPv2的配置及实现
  14. Untracked Files Prevent Pull解决方法
  15. 夜深人静写算法(五)- 初等数论
  16. 用C语言编程取出八个球,带你解决C语言编程问题~之口袋放球取法
  17. excel未保存强制关闭计算机,Excel文件未保存就关闭了,怎么恢复数据?
  18. Android Battery信息
  19. win7下如何注册控件
  20. HTTP的常用方法、GET和POST的区别

热门文章

  1. LINK : fatal error LNK1104: 无法打开文件“LIBCD.lib”
  2. 关于增强(五)-Class Enhancement
  3. 树状数组的区间修改+查询
  4. php 存储html 内容,HTML 本地存储
  5. 修改value_EXCEL批量名称修改
  6. 用栈解决四则运算问题
  7. linux命令 sed 有的功能有,Linux命令:sed简介
  8. 1470A. Strange Birthday Party
  9. 902. 最短编辑距离
  10. Android中怎获取json,Android应用中如何解析获取的json数据