http://poj.org/problem?id=2485

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.

最小生成树问题:(用prim算法)

/*题意:Flatopia岛要修路,这个岛上有n个城市,要求修完路后,各城市之间可以相互到达,且修的总
路程最短.
求所修路中的最长的路段*/

#include <iostream>
#include <stdio.h>
#include <string.h>
#define INF 0x3f3f3f3f
using namespace std;
int map[][];
int n,dis[],v[];
void prim()
{
int min,sum=-,k;
for(int i=; i<=n; i++)
{
v[i]=;
dis[i]=INF;
}
for(int i=; i<=n; i++)
dis[i]=map[][i];
v[]=;
for(int j=; j<n; j++)
{
min=INF;
for(int i=; i<=n; i++)
{
if(v[i]==&&dis[i]<min)
{
k=i;
min=dis[i];
}
}
if(sum<min)
sum=min;
v[k]=;
for(int i=; i<=n; i++)
{
if(v[i]==&&map[k][i]<dis[i])
{
dis[i]=map[k][i];
}
}
}
cout<<sum<<endl;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(int i=; i<=n; i++)
{
for(int j=; j<=n; j++)
{
scanf("%d",&map[i][j]);
}
}
prim();
}
return ;
}

POJ2485:Highways(模板题)的更多相关文章

  1. [AHOI 2009] 维护序列(线段树模板题)

    1798: [Ahoi2009]Seq 维护序列seq Time Limit: 30 Sec  Memory Limit: 64 MB Description 老师交给小可可一个维护数列的任务,现在小 ...

  2. HDU 2222 AC自动机模板题

    题目: http://acm.hdu.edu.cn/showproblem.php?pid=2222 AC自动机模板题 我现在对AC自动机的理解还一般,就贴一下我参考学习的两篇博客的链接: http: ...

  3. POJ2774 & 后缀数组模板题

    题意: 求两个字符串的LCP SOL: 模板题.连一起搞一搞就好了...主要是记录一下做(sha)题(bi)过程心(cao)得(dan)体(xin)会(qing) 后缀数组概念...还算是简单的,过程 ...

  4. HDU 1251 Trie树模板题

    1.HDU 1251 统计难题  Trie树模板题,或者map 2.总结:用C++过了,G++就爆内存.. 题意:查找给定前缀的单词数量. #include<iostream> #incl ...

  5. HDU-3549 最大流模板题

    1.HDU-3549   Flow Problem 2.链接:http://acm.hdu.edu.cn/showproblem.php?pid=3549 3.总结:模板题,参考了 http://ww ...

  6. HDU 4280:Island Transport(ISAP模板题)

    http://acm.hdu.edu.cn/showproblem.php?pid=4280 题意:在最西边的点走到最东边的点最大容量. 思路:ISAP模板题,Dinic过不了. #include & ...

  7. HDU-2222 Keywords Search(AC自动机--模板题)

    题目大意:统计一共出现了多少次模板串. 题目分析:AC自动机的模板题.不过这题有坑,相同的模板串不能只算一次. 代码如下: # include<iostream> # include< ...

  8. Dancing Link --- 模板题 HUST 1017 - Exact cover

    1017 - Exact cover Problem's Link:   http://acm.hust.edu.cn/problem/show/1017 Mean: 给定一个由0-1组成的矩阵,是否 ...

  9. AC自动机 - 多模式串匹配问题的基本运用 + 模板题 --- HDU 2222

    Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

随机推荐

  1. numpy常用举例

    转自https://morvanzhou.github.io/tutorials/data-manipulation/np-pd/2-1-np-attributes/ numpy 的属性: ndim: ...

  2. 架设FTP Server-Windows Server 2012

    架设FTP Server-Windows Server 2012 https://jingyan.baidu.com/article/03b2f78c75b9b65ea237ae84.html   在 ...

  3. sencha touch TabPanel 加入导航按钮(向左向右按钮) 以及Carousel插件(2014-11-7)

    Carousel插件代码: /* * TabPanel的Carousel功能插件 * 取至 * https://github.com/VinylFox/Ext.ux.touch.SwipeTabs * ...

  4. mysql select缓存使用详解

    mysql Query Cache 默认为打开.从某种程度可以提高查询的效果,但是未必是最优的解决方案,如果有的大量的修改和查询时,由于修改造成的cache失效,会给服务器造成很大的开销,可以通过qu ...

  5. 使用shell数据处理数据实例①-------手把手教学版

    引子: 在工作过程中经常要处理各种小数据,同事间会用各种工具方法来处理,比如用java.python.Perl甚至用UE手工处理.但貌似不都方便. 今天举一例子使用shell来处理,来说明shell一 ...

  6. Unity3D笔记 切水果三 切水果

    一.创建一个Apple00,拖拽Prfabs下的Sprite命名为Apple00,添加碰撞器Box Collider,编写代码Knife Rey1.js 二.做被切的苹果 步骤和一是一样的,但是被切的 ...

  7. mysql操作常用技巧

    删除一张表的数据,条件在另一张表 delete a from A a,B b where a.tel=b.tel and a.code=b.code and a.day='201808';

  8. Nginx 安装 --编译模块参数

    公司空出来一些服务器,很久没有来练手了,于是便开始有了这篇博客,记录下过程. Nginx 这个不多说了,名声在外,人们喜爱使用这款软件,主要还是因为它的高并发特性,公司也在用效果还不错,也用了它的一些 ...

  9. hdu1754 I Hate It【线段树】

    很多学校流行一种比较的习惯.老师们很喜欢询问,从某某到某某当中,分数最高的是多少.  这让很多学生很反感.  不管你喜不喜欢,现在需要你做的是,就是按照老师的要求,写一个程序,模拟老师的询问.当然,老 ...

  10. poj3417Network【LCA】【树形DP】

    Yixght is a manager of the company called SzqNetwork(SN). Now she's very worried because she has jus ...