Highways
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 23421   Accepted: 10826

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<stdio.h>
#include<string.h>
const int M = , inf = 0x3f3f3f3f ;
int T , n ;
int map[M][M] ;
int d[M] ;
int p[M] ; void prim ()
{
for (int i = ; i <= n ; i++) {
d[i] = map[][i] ;
p[i] = ;
}
d[] = ;
int ans = -inf ;
for (int i = ; i < n ; i++) {
int minc = inf , k ;
for (int j = ; j <= n ; j++) {
if (d[j] && d[j] < minc) {
minc = d[j] ;
k = j ;
}
}
d[k] = ;
for (int j = ; j <= n ; j++) {
if (d[j] && d[j] > map[k][j]) {
d[j] = map[k][j] ;
p[j] = k ;
}
}
if (ans < minc)
ans = minc ;
// printf ("minc=%d\n" , minc) ;
}
printf ("%d\n" , ans) ;
} int main ()
{
// freopen ("a.txt" , "r" , stdin) ;
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]) ;
if (i == j)
map[i][j] = inf ;
}
}
prim () ;
}
}

which is the length of longest to be built : 没正确理解,orz

Highways(prim & MST)的更多相关文章

  1. 【POJ 2485】Highways(Prim最小生成树)

    题目 Prim算法:任选一个点,加入集合,找出和它最近的点,加入集合,然后用加入集合的点去更新其它点的最近距离......这题求最小生成树最大的边,于是每次更新一下最大边. #include < ...

  2. c/c++ 用普利姆(prim)算法构造最小生成树

    c/c++ 用普利姆(prim)算法构造最小生成树 最小生成树(Minimum Cost Spanning Tree)的概念: ​ 假设要在n个城市之间建立公路,则连通n个城市只需要n-1条线路.这时 ...

  3. 普利姆算法(prim)

    普利姆算法(prim)求最小生成树(MST)过程详解 (原网址) 1 2 3 4 5 6 7 分步阅读 生活中最小生成树的应用十分广泛,比如:要连通n个城市需要n-1条边线路,那么怎么样建设才能使工程 ...

  4. 查找最小生成树:普里姆算法算法(Prim)算法

    一.算法介绍 普里姆算法(Prim's algorithm),图论中的一种算法,可在加权连通图里搜索最小生成树.意即由此算法搜索到的边子集所构成的树中,不但包括了连通图里的所有顶点,且其所有边的权值之 ...

  5. 无向图最小生成树(prim算法)

    普里姆算法(Prim算法),图论中的一种算法,可在加权连通图里搜索最小生成树.意即由此算法搜索到的边子集所构成的树中,不但包括了连通图里的所有顶点,且其所有边的权值之和亦为最小.该算法于1930年由捷 ...

  6. 最小生成树(prim)

    里姆算法(Prim算法),图论中的一种算法,可在加权连通图里搜索最小生成树.意即由此算法搜索到的边子集所构成的树中,不但包括了连通图里的所有顶点(英语:Vertex (graph theory)),且 ...

  7. 【SPOJ】Highways(矩阵树定理)

    [SPOJ]Highways(矩阵树定理) 题面 Vjudge 洛谷 题解 矩阵树定理模板题 无向图的矩阵树定理: 对于一条边\((u,v)\),给邻接矩阵上\(G[u][v],G[v][u]\)加一 ...

  8. HDU.1233 还是畅通工程(Prim)

    HDU.1233 还是畅通工程(Prim) 题意分析 首先给出n,代表村庄的个数 然后出n*(n-1)/2个信息,每个信息包括村庄的起点,终点,距离, 要求求出最小生成树的权值之和. 注意村庄的编号从 ...

  9. 最小生成树算法详解(prim+kruskal)

    最小生成树概念: 一个有 n 个结点的连通图的生成树是原图的极小连通子图,且包含原图中的所有 n 个结点,并且有保持图连通的最少的边. 最小生成树可以用kruskal(克鲁斯卡尔)算法或prim(普里 ...

随机推荐

  1. 学习笔记——Maven实战(八)常用Maven插件介绍(下)

    我们都知道Maven本质上是一个插件框架,它的核心并不执行任何具体的构建任务,所有这些任务都交给插件来完成,例如编译源代码是由maven- compiler-plugin完成的.进一步说,每个任务对应 ...

  2. GCC:条件判断中赋值语句和函数结尾时无返回值的警告

    有下面非常经典的一个字符串复制程序. test1.c #include <stdio.h> int main() { char str_t[]="This String come ...

  3. Java并发编程-ReentrantLock

    代码示例: Lock lock = new ReentrantLock(); lock.lock(); try { // update object state } finally { lock.un ...

  4. tsql的奇特语法

    也许是离开t-sql太久了,突然发现很多t-sql的奇特语法 用一句sql解决多种排序: ; SELECT C1, C2 FROM T ORDER BY THEN C1 END ASC, THEN C ...

  5. [wikioi1553]互斥的数(数学分析+散列/数学分析+二分)

    题目描述 Description 有这样的一个集合,集合中的元素个数由给定的N决定,集合的元素为N个不同的正整数,一旦集合中的两个数x,y满足y = P*x,那么就认为x,y这两个数是互斥的,现在想知 ...

  6. NoSQL数据库之国产开源产品:SequoiaDB 分析前言

    随着互联网技术的发展,面对海量数据的存储和分析,传统关系型数据库已经无法满足,由此衍生出一种与关系型数据库区别开的数据库NoSQL(Not Only SQL). 国外做的比较成熟的NoSQL有Mong ...

  7. formData_html5_map标签

    1 : //更省事 var files = fileInput.files; var formData = new FormData(); //将所有文件插入formData formData .ap ...

  8. poj2485&&poj2395 kruskal

    题意:最小生成树的最大边最小,sort从小到大即可 poj2485 #include<stdio.h> #include<string.h> #include<algor ...

  9. sql- 别名alias(as)

    alias (别名) 在 SQL 上的用处.最常用到的别名有两种: 栏位别名及表格别名. 简单地来说,栏位别名的目的是为了让 SQL 产生的结果易读.在之前的例子中,每当我们有营业额总合时,栏位名都是 ...

  10. Java基础-JVM

    jvm=> java虚拟机 一.java虚拟机的生命周期: Java虚拟机的生命周期 一个运行中的Java虚拟机有着一个清晰的任务:执行Java程序.程序开始执行时他才运行,程序结束时他就停止. ...