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.

Source

POJ Contest,Author:Mathematica@ZSU
 
 
 
 
 

#include<stdio.h>
#include<string.h>
int map[505][505];
int dis[505],vis[505];
int n;
int prim(int u){
int tmax=0;
for(int i=1;i<=n;i++){
dis[i]=map[u][i];
}
vis[u]=1;
for(int k=1;k<n;k++){
int tmin=999999999;
int temp;
for(int j=1;j<=n;j++){
if(dis[j]<tmin&&!vis[j]){
tmin=dis[j];
temp=j;
}
}
vis[temp]=1;
if(tmin>tmax)
tmax=tmin;
for(int i=1;i<=n;i++){
if(dis[i]>map[temp][i]&&!vis[i])
dis[i]=map[temp][i];
}
}
return tmax;
}
int main(){
int t;
scanf("%d",&t);
while(t--){
memset(vis,0,sizeof(vis));
memset(dis,0,sizeof(dis));
memset(map,0,sizeof(map));
scanf("%d",&n);
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
scanf("%d",&map[i][j]);
}
}
printf("%d\n",prim(1));
printf("\n");
}
return 0;
}

poj2485 Highways的更多相关文章

  1. POJ2485——Highways

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

  2. POJ2485:Highways(模板题)

    http://poj.org/problem?id=2485 Description The island nation of Flatopia is perfectly flat. Unfortun ...

  3. POJ2485 Highways 【MST】

    Highways Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22842   Accepted: 10525 Descri ...

  4. POJ2485 Highways(最小生成树)

    题目链接. 分析: 比POJ2253要简单些. AC代码: #include <iostream> #include <cstdio> #include <cstring ...

  5. 最小生成树Prim poj1258 poj2485 poj1789

    poj:1258 Agri-Net Time Limit: 1000 MS Memory Limit: 10000 KB 64-bit integer IO format: %I64d , %I64u ...

  6. POJ 1258 Agri-Net|| POJ 2485 Highways MST

    POJ 1258 Agri-Net http://poj.org/problem?id=1258 水题. 题目就是让你求MST,连矩阵都给你了. prim版 #include<cstdio> ...

  7. MST系列

    1.POJ2485 Highways 蛮水的 数组一开始开小了卡了一会儿 我可能是个傻逼 #include<iostream> #include<cstdio> #includ ...

  8. poj图论解题报告索引

    最短路径: poj1125 - Stockbroker Grapevine(多源最短路径,floyd) poj1502 - MPI Maelstrom(单源最短路径,dijkstra,bellman- ...

  9. H:Highways

    总时间限制: 1000ms 内存限制: 65536kB描述The island nation of Flatopia is perfectly flat. Unfortunately, Flatopi ...

随机推荐

  1. asp.net 预编译和动态编译

    在asp.net中,编译可以分为:动态编译Dynamical Compilation和预编译(Precompilation). 动态编译 深入剖析ASP.NET的编译原理之一:动态编译(Dynamic ...

  2. IOS 遍历未知对象的属性和方法

    /* 注意:要先导入ObjectC运行时头文件,以便调用runtime中的方法*/ #import <objc/runtime.h> @implementation NSObject (P ...

  3. Customizing Navigation Bar and Status Bar

    Like many of you, I have been very busy upgrading my apps to make them fit for iOS 7. The latest ver ...

  4. OVER(PARTITION BY)函数介绍

    问题场景 最近在项目中遇到了对每一个类型进行求和并且求该类型所占的比例,当时考虑求出每种类型的和,并在java中分别对每一种类型的和与总和相除求出所占比例.后来,想到这样有点麻烦,并且项目中持久层使用 ...

  5. 在Myeclipse buildpath 加server lib (server runtime)/项目导入时报错:The import javax.servlet.http.HttpServletRequest cannot be resolved

    来源于:http://blog.csdn.net/dingqinghu/article/details/8805922 http://yl-fighting.iteye.com/blog/140946 ...

  6. OC基础--OC内存管理原则和简单实例

    ARC: 由于自己的学习视频太早,Xcode是iOS6版本,新建命令行项目后,系统会默认启动ARC机制,全程Automatic Reference Counting,简单的说,就是代码中自动加入了re ...

  7. nslog

    今天有人问我怎么更好的使用nslog,打包的时候老注释 pch里加下面的代码就好了平时debug的时候打印,release后就不打印了 #ifdef DEBUG#define NSLog(...) N ...

  8. 【CodeForces 557B】Pasha and Tea

    题 题意 总共有 w 克蛋糕,2n 个盘子,第 i 个盘子容量为 ai ,n 个女孩和 n 个男孩,男孩得到的是女孩得到的蛋糕的两倍,求他们得到蛋糕的最大值. 分析 把盘子从小到大排序,然后 女生得到 ...

  9. POJ3628 Bookshelf 2(01背包+dfs)

    Bookshelf 2 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8745   Accepted: 3974 Descr ...

  10. 添加已有项目到git rep

    cd yourproject——homegit init //在当前项目目录中生成本地git管理,建立一个隐藏.git目录 git add src //添加你想用git管理的代码的目录 git com ...