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. JetBrains IntelliJ IDEA for Mac 15.0 破解版 – Mac 上强大的 Java 集成开发工具

    应网友要求更新. IntelliJ IDEA 是最强大的 Java IDE 之一,由知名的Jetbrainsg公司出品,最新版本增加了大量强大易用的特性,比如 Java 8 的Lambda 表达式调试 ...

  2. perl5 第七章 控制结构

    第七章 控制结构 by flamephoenix 一.条件判断二.循环:  1.while循环   2.until循环   3.for循环   4.针对列表(数组)每个元素的foreach循环  5. ...

  3. JAVA FILE or I/O学习 - I/O流操作:FileInputStream、FileOutputStream、ObjectInputStream、ObjectOutputStream、InputStreamReader、OutputStreamWriter等

    public class IOStreamKnow { /*********************************文件读写方式:字节流**************************** ...

  4. $.getJSON(url,function success(){})回调函数不起作用

    有个问题好久没有解决,就是: $.getJSON(url,function success(){}) 其中的回调函数,总也不执行. 以前也做过,但那都是CTRL+C,CTRL+V,也没有细想. 目标就 ...

  5. poj 2051 Argus(优先队列)

    题目链接: http://poj.org/problem?id=2051 思路分析: 优先级问题,使用优先队列求解:当执行某个任务后,再增加一个任务到队列中, 该任务的优先级为执行任务的时间加上其时间 ...

  6. POJ 1655 - Balancing Act 树型DP

    这题和POJ 3107 - Godfather异曲同工...http://blog.csdn.net/kk303/article/details/9387251 Program: #include&l ...

  7. 最新VMware Workstation 10注册码,绝对可用!

    最近公司要在solaris上测试产品,需要用到虚拟机,于是下载了最新的虚拟机VMware Workstation 10,并找到了破解码,与大家共享: VMware workstation 10破解序列 ...

  8. 11个实用但你可能不知道的Python程序库

    目前,网上已有成千上万个Python包,但几乎没有人能够全部知道它们.单单PyPi上就有超过47000个包列表. 现在,越来越多的数据科学家开始使用Python,虽然他们从pandas,scikit- ...

  9. [译]MDX 介绍

    关于MDX MDX (Multi Dimensional eXpression language) 是非常强大的工具,可以将你的多维数据库/cube 发挥到极致. 本文会覆盖MDX基础,并且希望能使你 ...

  10. 个人自建网店(WordPress WooCommerce on SAE)集成支付宝支付

    插件: Alipay For WooCommerce 到支付宝账户的商家服务提出申请: https://b.alipay.com/order/serviceIndex.htm 在"在线签约- ...