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. SNAT DNAT MASQUERADE 区别

    SNAT,DNAT,MASQUERADE都是NATMASQUERADE是SNAT的一个特例SNAT是指在数据包从网卡发送出去的时候,把数据包中的源地址部分替换为指定的IP,这样,接收方就认为数据包的来 ...

  2. spring cache 详解

    Spring使用Cache 从3.1开始,Spring引入了对Cache的支持.其使用方法和原理都类似于Spring对事务管理的支持.Spring Cache是作用在方法上的,其核心思想是这样的:当我 ...

  3. SharpGL学习笔记(五) 视口变换

    视口变换主是将视景体内投影的物体显示到二维的视口平面上. 在计算机图形学中,它的定义是将经过几何变换, 投影变换和裁剪变换后的物体显示于屏幕指定区域内. 前面我们讨论过的透视投影, 正射投影, 它们都 ...

  4. 部署OpenStack问题汇总(六)-- OpenStack入门需要知道的概念

    本博客已经添加"打赏"功能,"打赏"位置位于右边栏红色框中,感谢您赞助的咖啡. 一.网络问题-network 更多网络原理机制可以参考<OpenStack ...

  5. C语言位操作--逻辑运算符组合

    假设读者熟悉普通代数与布尔代数,下面是部分常见的涉及到加法.减法与逻辑运算符的组合: a.        -x=~x+1 b.           =~(x-1) c.        ~x=-x-1 ...

  6. Unix/Linux 查看文件大小

    ls -l help.html-rw-r--r--  1 william  wheel  40960 Jul 18 00:59 development.sqlite3 40960 就是文件的大小. d ...

  7. Hive show

    CREATE TABLE page_view(viewTime INT, userid BIGINT,p_date timestamp, page_url STRING, referrer_url v ...

  8. poj3259 Wormholes【最短路-bellman-负环】

    While exploring his many farms, Farmer John has discovered a number of amazing wormholes. A wormhole ...

  9. vs中如何添加库目录、包含目录以及依赖-----转

    在生成时,可能需要首先生成某些项目,以便生成由其他项目使用的可执行代码.使用 “解决方案属性页”对话框 ->“通用属性”->“项目依赖项” 设置当前生成顺序.若要访问此对话框,请在“解决方 ...

  10. Java Eclipse编译后产生的字节码文件,用DOS命令符怎么打开

    在很多初学者刚刚接触eclipse的时候,写完一个代码文件.例如 Demo.java 通过run as a java application生成之后,会产生一个Demo.class. Demo.cla ...