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. axios中为所有请求带上Token头

    axios中为所有请求带上Token头 https://www.imooc.com/article/27751

  2. AspNetCore容器化(Docker)部署(二) —— 多容器通信

    一.前言 着上一篇 AspNetCore容器化(Docker)部署(一) —— 入门,在单个容器helloworld的基础上引入nginx反向代理服务器组成多容器应用. 二.配置反向代理转接 配置转接 ...

  3. poj1681 Painter's Problem

    题目描述: 和那道关灯差不多,求最少涂几次. 题解: 高消,然后深搜枚举自由元更新答案. 貌似这道题没卡贪心但是其他题基本都卡了. 比如$Usaco09Nov$的$lights$ 代码: #inclu ...

  4. mysql 报错Authentication method 'caching_sha2_password' is not supported

    原文地址:https://blog.csdn.net/u011583336/article/details/80999043 之前工作中用的数据库多是ms sqlserver,偶尔用到mysql都是运 ...

  5. phpstudy里升级mysql版本到5.7

    phpstudy里没有地方可以设置mysql数据库,很多人都疑惑在phpstudy里怎么升级mysql数据库版本,本文就教你如何在phpstudy中升级mysql的版本. PhpStudy集成环境中的 ...

  6. php中session和cookie的使用及区别

    1.cookie的使用 什么是 Cookie? cookie 常用于识别用户.cookie 是服务器留在用户计算机中的小文件.每当相同的计算机通过浏览器请求页面时,它同时会发送 cookie.通过 P ...

  7. python-----定制群发微信消息

    如何使用表格中的信息群发微信消息? 如何读取csv? →   使用内置模块csv 如何按对应信息发送到微信?→  使用第三方库wxpy 以下代码素材自取:链接:https://pan.baidu.co ...

  8. Ubuntu16.04安装MySql5.7

    安装方式有好多种,这里选择使用APT安装. 主要参考文档为官方文档:https://dev.mysql.com/doc/mysql-apt-repo-quick-guide/en/#apt-repo- ...

  9. 利用virtualbox中的虚机制作主机启动盘

    制作镜像的过程: 第一步:1.Windows下先下载安装virtualbox usb3.0驱动:https://download.virtualbox.org/virtualbox/5.2.20/Or ...

  10. 大数据学习——mapreduce倒排索引

    数据 a.txt hello jerry hello tom b.txt allen tom allen jerry allen hello c.txt hello jerry hello tom 1 ...