Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 24383   Accepted: 11243

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

Hint

Huge input,scanf is recommended.
注意提示,开始我用的cin,cout输入输出,结果超时,看到提示用的scanf就AC了
 #include <stdio.h>
#define INF 65537;
int map[][];
int vis[];
int dis[];
int n;
int Prim(){
for(int i=;i<n;i++){
vis[i]=;
dis[i]=INF;
}
dis[]=;
for(int i=;i<n;i++){
int p;
int mine=INF;
for(int j=;j<n;j++){
if(!vis[j]&&mine>dis[j]){
p=j;
mine=dis[j];
}
}
vis[p]=;
for(int j=;j<n;j++){
if(!vis[j]&&dis[j]>map[p][j])
dis[j]=map[p][j];
}
}
int maxe=;
for(int i=;i<n;i++){
if(maxe<dis[i])
maxe=dis[i];
}
return maxe;
}
int main() { int num;
scanf("%d",&num);
for(int i=;i<num;i++){
scanf("%d",&n);
for(int j=;j<n;j++){
for(int k=;k<n;k++){
scanf("%d",&map[j][k]);
}
}
int maxe=Prim();
printf("%d\n",maxe);
}
return ;
}

Highways - poj 2485 (Prim 算法)的更多相关文章

  1. Highways POJ-1751 最小生成树 Prim算法

    Highways POJ-1751 最小生成树 Prim算法 题意 有一个N个城市M条路的无向图,给你N个城市的坐标,然后现在该无向图已经有M条边了,问你还需要添加总长为多少的边能使得该无向图连通.输 ...

  2. Highways POJ 2485【Prim】

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

  3. Highways poj 2485

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

  4. poj 2485 (kruskal算法)

    /*kruskal算法*/ #include <iostream> //#include <fstream> #include <algorithm> using ...

  5. Agri-Net - poj 1258 (Prim 算法)

      Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 44373   Accepted: 18127 Description F ...

  6. Truck History - poj 1789 (Prim 算法)

      Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 20884   Accepted: 8075 Description Ad ...

  7. POJ 2485 Prim 找最长的边

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

  8. 【POJ 2485】Highways(Prim最小生成树)

    题目 Prim算法:任选一个点,加入集合,找出和它最近的点,加入集合,然后用加入集合的点去更新其它点的最近距离......这题求最小生成树最大的边,于是每次更新一下最大边. #include < ...

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

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

随机推荐

  1. [LOJ6208]树上询问

    题目大意: 有一棵n节点的树,根为1号节点.每个节点有两个权值ki,ti,初始值均为0. 给出三种操作: 1.Add(x,d)操作:将x到根的路径上所有点的ki←ki+d 2.Mul(x,d)操作:将 ...

  2. [HDU6240]Server

    题目大意: 用$n$条线段覆盖区间$[1,t]$上的整点.每条线段有4个属性$(S_i,T_i,A_i,B_i)$,表示用第$i$条线段可以覆盖区间$[S_i,T_i]$.若选取线段的集合为$S$,最 ...

  3. 六. 异常处理10.Java的内置异常

    在标准包java.lang中,Java定义了若干个异常类.前面的例子曾用到其中一些.这些异常一般是标准类RuntimeException的子类.因为java.lang实际上被所有的Java程序引入,多 ...

  4. C语言基础之注释与常见错误

    总结起来,注释有三种: 1.单行注释 1: //哈哈 单行注释 2.多行注释 1: /* 2: asdfasdfasdfasdfasdf 3: */ 其中多行注释如果这样写 1: /* 2: * 函数 ...

  5. SQL SERVER 内存学习系列

    http://www.cnblogs.com/double-K/p/5049417.html http://blog.sina.com.cn/s/blog_5deb2f5301014wti.html ...

  6. UBI - Unsorted Block Images

    参考:http://www.linux-mtd.infradead.org/doc/ubi.html UBI - Unsorted Block Images Table of contents Big ...

  7. VBA 时间相关的函数

    DateSerial DateADD Datediff http://www.yiibai.com/vba/vba_datediff_function.html https://www.techont ...

  8. selenium清空元素时,.clear不执行

    应该是由于鼠标焦点没有定位到相应元素 driver.find_element_by_xpath('//input[@type="password"]').click() drive ...

  9. 用DotSpatial下载谷歌瓦片图并展示到地图控件上 【转】

    http://blog.csdn.net/caoshiying/article/details/51991647 上一篇文章讲解如何加载各地图的WMS地图服务.虽然不涉及到瓦片,但是每次地图刷新都要请 ...

  10. README.md文档

    大标题 =================================== 大标题一般显示工程名,类似html的\<h1\> 你只要在标题下面跟上=====即可 中标题 ------- ...