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.
分析:最小生成树求最大权值~
源码:
#include<iostream>
#include<string.h>
#include<stdio.h>
#include<ctype.h>
#include<algorithm>
#include<stack>
#include<queue>
#include<set>
#include<math.h>
#include<vector>
#include<deque>
#include<list>
using namespace std;
int n;
int i,j;
int map[550][550];
bool mark[550];
int prim()
{
int w=n;
int k;
int len;
int max=-1;
while(--w)
{
int min=999999;
int y=0;
for(i=2;i<=n;i++)
{
if(!mark[i]&&map[1][i]<min)
{
min=map[1][i];
k=i;
}
}
mark[k]=1;
if(max<min)
max=min;
for(i=2;i<=n;i++)
{
if(!mark[i]&&map[k][i]<map[1][i])
map[1][i]=map[k][i];
}
}
return max;
}
int main()
{
int test;
scanf("%d",&test);
while(test--)
{
memset(map,0,sizeof(map));
memset(mark,0,sizeof(mark));
scanf("%d",&n);
for(i=1; i<=n; i++)
for(j=1; j<=n; j++)
scanf("%d",&map[i][j]);
printf("%d\n",prim());
}
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. A2W和W2A :很好的多字节和宽字节字符串的转换宏

    以前看<Window核心编程>,感觉多字节和宽字节之间还比较麻烦的,至少MultiByteToWideChar函数和WideCharToMultiByte函数有足够多的参数的意义让我们去理 ...

  2. Spring 拦截器实现事物

    Spring+Hibernate的实质:就是把Hibernate用到的数据源Datasource,Hibernate的SessionFactory实例,事务管理器HibernateTransactio ...

  3. java循环HashMap两种方法的效率比较

    一.循环HashMap的两种方式 方式1: Iterator<Entry<String, String>> entryKeyIterator = entrySetMap.ent ...

  4. [Python]Unicode转ascii码的一个好方法

    写这篇文章的是一位外国人,他遇到了什么问题呢?比如有一个 Unicode 字符串他需要转为 ascii码: >>> title = u"Klüft skräms inför ...

  5. 什么是Thrift

    起源 百度百科怎么说 thrift是一个软件框架,用来进行可扩展且跨语言的服务的开发. 它结合了功能强大的软件堆栈和代码生成引擎,以构建在 C++, Java, Python, PHP, Ruby, ...

  6. EF使用时异常:对一个或多个实体的验证失败。有关详细信息

    //最顶级异常中是不提示具体哪个字段验证失败,必须到详细异常类型中查看 try { //EF操作 } catch (System.Data.Entity.Validation.DbEntityVali ...

  7. NGUI 按钮音效问题

    昨天给NGUI的按钮添加音效时,刚开始是自己新建空对象绑定声音的,后来发现NGUI按钮携带button sound组件,直接将音效拖入即可,不用写一行代码,非常简单.但是后来发现添加相同的音效有的按钮 ...

  8. UIWebvView 解决onClick 延迟相应问题

    在使用 UIWebView 的过程中, 发现 onClick 触发需要等待300-500ms, Google了一下, 发现是因为ScrollView 在等待doubleTap, 所以有延迟 使用如下代 ...

  9. Android 开发技巧

    1, ActionBar.setTitle() 的参数是 CharSequence,可以用SpannableString的,颜色字形随你

  10. wdcp centos6.5多网站部署

    一.wdcp介绍 wdCP是WDlinux Control Panel的简称,是一套通过WEB控制和管理服务器的Linux服务器管理系统以及虚拟主机管理系统,旨在易于使用Linux系统做为我们的网站服 ...