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

#include<stdio.h>
#include<string.h>
#define INF 0x3f3f3f3f
#define N 550
int n;
int i,j;
int map[N][N];
int a,b;
int low[N];
bool vis[N];
int sum[N];
void input()
{
//memset(map,INF,sizeof(map));
for(i=1;i<=n;++i)
{
for(j=1;j<=n;++j)
{
scanf("%d",map[i]+j);
}
}
}
void prim()
{
int pos=1;
for(i=1;i<=n;++i)//第一次给low赋值
{
low[i]=map[pos][i];
}
vis[pos]=1; //增加最小生成树集合
for(i=1;i<n;++i)//再找n-1个点
{
int min=INF;
for(j=1;j<=n;++j)
{
if(!vis[j]&&min>low[j])
{
min=low[j];
pos=j;//把找到的点记录下
}
}
sum[i]=min;
vis[pos]=1;
for(j=1;j<=n;++j)
{
if(!vis[j]&&low[j]>map[pos][j])
{
low[j]=map[pos][j];
}
}
}
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
memset(vis,0,sizeof(vis));
input();
prim();
int max=-INF;
for(i=1;i<n;++i)
if(max<sum[i])
max=sum[i];
printf("%d\n",max);
}
return 0;
}

Highways POJ 2485【Prim】的更多相关文章

  1. poj2728 Desert King【最优比率生成树】【Prim】【0/1分数规划】

    含[最小生成树Prim]模板. Prim复杂度为$O(n^2),适用于稠密图,特别是完全图的最小生成树的求解.   Desert King Time Limit: 3000MS   Memory Li ...

  2. 【简●解】POJ 1845 【Sumdiv】

    POJ 1845 [Sumdiv] [题目大意] 给定\(A\)和\(B\),求\(A^B\)的所有约数之和,对\(9901\)取模. (对于全部数据,\(0<= A <= B <= ...

  3. Highways - poj 2485 (Prim 算法)

      Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 24383   Accepted: 11243 Description T ...

  4. 洛谷1546 最短网络Agri-Net【最小生成树】【prim】

    [内含最小生成树Prim模板] 题目:https://www.luogu.org/problemnew/show/P1546 题意:给定一个邻接矩阵.求最小生成树. 思路:点少边多用Prim. Pri ...

  5. POJ 2154 【POLYA】【欧拉】

    前记: TM终于决定以后干啥了.这几天睡的有点多.困饿交加之间喝了好多水.可能是灌脑了. 切记两件事: 1.安心当单身狗 2.顺心码代码 题意: 给你N种颜色的珠子,串一串长度问N的项链,要求旋转之后 ...

  6. Highways poj 2485

    Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public h ...

  7. POJ 3230 【DP】

    题意: 某货旅行,在n个城市呆m天. 给出从第i个城市到第j个城市的路费,或者留在某个城市的生活费. 给出在第i天在第j个城市的收益. 可以在城市之间任意穿梭逗留没有其他特殊要求. 求收益最大是多少. ...

  8. H - Highways - poj 1751(prim)

    某个地方政府想修建一些高速公路使他们每个乡镇都可以相同通达,不过以前已经修建过一些公路,现在要实现所有的联通,所花费的最小代价是多少?(也就是最小的修建长度),输出的是需要修的路,不过如果不需要修建就 ...

  9. POJ 3187【permutation】

    POJ 3187 给定N值,从而确定了数据的范围及长度,暴力枚举数列,接下来类似杨辉三角的递推计算.注permutation从递增有序数列开始枚举,枚举到符合sum值时退出即可 #include &l ...

随机推荐

  1. 小程序08 小程序访问服务器API

    后台交互 小程序是前端框架,需要和后台交互,本次课程主要介绍网络API. 小程序提供的网络访问API wx.request接口 发起 HTTPS 网络请求. 使用rqeust接口前的工作 1.小程序需 ...

  2. mysql 根据月份查找数据

  3. Oracle————存储过程与函数

    存储过程存储过程参数模式包括IN.OUT. IN OUT. IN(默认参数模式):表示当存储过程别调用时,实参值被传递给形参:形参起变量作用,只能读该参数,而不能修改该参数.IN模式参数可以是变量或表 ...

  4. vue.js中的表单radio,select,textarea的v-model属性的用法

    只要是表单元素,其值已经不会再用value来定义了,但是placeholder还是可以用来设置默认值. section1--input:type="text" type=" ...

  5. 122. Best Time to Buy and Sell Stock II@python

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  6. [模板] Exgcd

    求解一组ax+bc=gcd(a,b) #include<iostream> #include<cstdio> using namespace std; int exgcd(in ...

  7. 利用RestTemplate进行http调用

    在对接API的时候,会涉及调用第三方的服务,这时候可以利用RestTemplate进行调用,下面给大家展示一个简单的调用demo. package com.tanlu.user.api.control ...

  8. c++_核桃的数量

    #include <iostream> using namespace std; int gcd(int x,int y){ int temp; ){ temp=x%y; x=y; y=t ...

  9. 条款33:避免遮掩继承而来的名称(Avoiding hiding inherited names)

    NOTE: 1.derived classes 内的名称会遮掩base classes内的名称.在public继承下从来没有人希望如此. 2.为了让被遮掩的名称再见天日,可使用using 声明方式或转 ...

  10. 条款20:宁以pass-by-reference-to-const替换pass-by-value(Prefer pass-by-reference-to-const to pass-by-value)

    NOTE: 1.尽量以pass-by-reference-to-const 替换pass-by-value.前者通常比较高效,并可避免切割问题(slicing problem). 2.以上规则并不适用 ...