# Highways

 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 27781 Accepted: 12690

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
/*
* 文件名称：prim.cpp
* 作    者：单昕昕
* 完成日期：2016年6月1日
* 版 本 号：v1.0
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <algorithm>
#define MAXN 1010
#define INF 0xfffffff//0X代表16进制，后面是数字，十进制是4294967295
using namespace std;
int cost[MAXN][MAXN],dis[MAXN],mincost[MAXN],n,t;
bool used[MAXN];//标识是否使用过

void prim()
{
fill(mincost,mincost+n,INF);
fill(used,used+n,false);
mincost[0]=0;
int res=0,Max=-1;
while(true)
{
int v=-1;
for(int u=0; u<n; ++u)
{//从不属于已加入生成树的顶点中选取从已加入生成树的点到该顶点的权值最小的点
if(!used[u]&&(v==-1||mincost[u]<mincost[v]))
v=u;
}
if(v==-1) break;
used[v]=true;
Max=max(Max,mincost[v]);
//res+=mincost[v];
for(int u=0; u<n; ++u)
mincost[u]=min(mincost[u],cost[v][u]);
}
cout<<Max<<endl;
}

int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int t;
cin>>t;
while(t--)
{
cin>>n;
/*for(int i=0; i<n; ++i)
for(int j=0; j<n; ++j)
cost[i][j]=INF;*/
for(int i=0; i<n; i++)
for(int j=0; j<n; ++j)
cin>>cost[i][j];
prim();
}
return 0;
}
/*
1

3
0 990 692
990 0 179
692 179 0
*/

②kruskal

/*
* 文件名称：prim.cpp
* 作    者：单昕昕
* 完成日期：2016年3月30日
* 版 本 号：v1.0
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <algorithm>
#define MAXN 10010
#define INF 0xfffffff//0X代表16进制，后面是数字，十进制是4294967295
using namespace std;
int cost[MAXN][MAXN],dis[MAXN],pre[MAXN],n,cnt;
bool used[MAXN];//标识是否使用过

struct edge
{
int u,v,cost;
};
bool cmp(const edge &e1,const edge &e2)
{
return e1.cost<e2.cost;
}
edge es[MAXN];
int V,E;//顶点数和边数

int set_find(int x)
{
while(x!=pre[x])
x=pre[x];
return x;
}
void unite(int p,int q)
{
p=set_find(p);
q=set_find(q);
if(p!=q) pre[p]=q;
}
bool same(int x,int y)
{
return set_find(x)==set_find(y);
}
void init_union_find(int n)
{
for(int i=0; i<=n; i++)
pre[i]=i;
}
void kruskal()
{
sort(es,es+cnt,cmp);//按照edge.cost的顺序升序排列
init_union_find(V);//并查集初始化
//int res=0;
int Max=-1;
for(int i=0; i<E; ++i)
{
edge e=es[i];
if(!same(e.u,e.v))
{
unite(e.u,e.v);
//res+=e.cost;
Max=max(Max,e.cost);
}
}
cout<<Max<<endl;
}

int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int t;
cin>>t;
while(t--)
{
cin>>n;
cnt=0;
/*for(int i=0; i<n; ++i)
for(int j=0; j<n; ++j)
cost[i][j]=INF;*/
for(int i=0; i<n; i++)
for(int j=0; j<n; ++j)
{
es[cnt].u=i;
es[cnt].v=j;
cin>>es[cnt++].cost;
}
V=n;
E=n*n;
kruskal();
}
return 0;
}
/*
1

3
0 990 692
990 0 179
692 179 0
*/

• 本文已收录于以下专栏：

## poj1287 Networking 最小生成树模板题。prim+kruskal算法AC

You are assigned to design network connections between certain points in a wide area. You are given ...
• Lionel_D
• 2015年03月26日 15:09
• 896

## 最小生成树 Prim以及Kruskal算法及效率解析

• wr132
• 2015年02月01日 19:57
• 1703

## 最小生成树Prim与Kruskal算法的比较

• Mollnn
• 2016年09月19日 21:48
• 3796

## HDU 1301 &POJ 1215 Jungle Roads【最小生成树，Prime算法+Kruskal算法】

Jungle Roads Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) T...
• hurmishine
• 2016年08月04日 21:39
• 1675

## 最小生成树——Prim、Kruskal、Sollin(Boruvka)

• xiedeacc
• 2014年06月02日 23:47
• 1046

## poj2253 最小生成树中的最大边 prim

Frogger Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 30620   Accepted: 9875 ...
• became_a_wolf
• 2015年08月19日 09:15
• 730

## 算法导论--最小生成树（Kruskal和Prim算法）

• luoshixian099
• 2016年07月14日 16:58
• 13765

## POJ 2485-Highways（最小生成树prim）

Highways Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22433   Acce...
• qq_16255321
• 2014年08月21日 17:09
• 749

## 最小生成树总结(prim、并查集和kruskal) C++实现

• bobkentblog
• 2015年09月27日 03:44
• 1498

## POJ：1751 Highways（最小生成树kruskal算法）

Highways Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 13211   Acce...
• zugofn
• 2016年08月03日 21:32
• 339

举报原因： 您举报文章：POJ 2485-Highways(最小生成树裸题-prim/kruskal) 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)