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

题意: 先输入T,有T组数据。再输入有N个点。N行N列 每一个数据代表I点到J点的距离。求完毕一个 最小生成树种 的  最大边。


#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
#define q 505
int fa[q];
int map[q][q];
struct node{
int x,y,l;
}q[q*q/2];
int find(int x)
{
return x==fa[x]? x:find(fa[x]);
}
int cmp(node a,node b)
{
return a.l<b.l;
}
int main()
{
int k,t,n,i,j,max1;
scanf("%d",&t);
while(t--)
{
max1=0;
k=0;
scanf("%d",&n);
for(i=1;i<=n;i++)
{
fa[i]=i;
for(j=1;j<=n;j++)
{
scanf("%d",&map[i][j]);
if(i<j) // 优化:去掉重边还有中间的0
{
k++; //不知道有多少边。这样统计
q[k].x=i;
q[k].y=j;
q[k].l=map[i][j];
}
}
}
sort(q+1,q+1+k,cmp); //依据每条边的距离排序
for(i=1;i<=k;i++)
{
if(fa[find(q[i].x)]!=fa[find(q[i].y)]) //并查集思想。。 {
fa[find(q[i].x)]=find(q[i].y);
if(q[i].l>max1)
max1=q[i].l;
}
}
cout<<max1<<endl;
}
return 0;
}

Kruskal 模板。。

POJ 2485 Highways 最小生成树 (Kruskal)的更多相关文章

  1. POJ 2485 Highways(最小生成树+ 输出该最小生成树里的最长的边权)

                                                                                                         ...

  2. poj 2485 Highways 最小生成树

    点击打开链接 Highways Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19004   Accepted: 8815 ...

  3. poj 2485 Highways (最小生成树)

    链接:poj 2485 题意:输入n个城镇相互之间的距离,输出将n个城镇连通费用最小的方案中修的最长的路的长度 这个也是最小生成树的题,仅仅只是要求的不是最小价值,而是最小生成树中的最大权值.仅仅须要 ...

  4. poj 2485 Highways

    题目连接 http://poj.org/problem?id=2485 Highways Description The island nation of Flatopia is perfectly ...

  5. POJ 2485 Highways【最小生成树最大权——简单模板】

    链接: http://poj.org/problem?id=2485 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...

  6. POJ 2485 Highways( 最小生成树)

    题目链接 Description The islandnation of Flatopia is perfectly flat. Unfortunately, Flatopia has no publ ...

  7. POJ 2485 Highways (求最小生成树中最大的边)

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

  8. POJ 2485 Highways (prim最小生成树)

    对于终于生成的最小生成树中最长边所连接的两点来说 不存在更短的边使得该两点以不论什么方式联通 对于本题来说 最小生成树中的最长边的边长就是使整个图联通的最长边的边长 由此可知仅仅要对给出城市所抽象出的 ...

  9. poj 2485 Highways(最小生成树,基础,最大边权)

    题目 //听说听木看懂之后,数据很水,我看看能不能水过 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stri ...

随机推荐

  1. VC++常见错误原因解析--error LNK2019: 无法解析的外部符号 "public: void __thiscall

    根据个人遇到这个错误时的记录,原因可以分为一下几种: 原因一: 只是在.h里面声明了某个方法, 没有在cpp里面实现 . 具体讲,有时候在头文件中声明了需要的方法,确实忘记了在源文件中实现: 有时候在 ...

  2. 北工大2017校赛 1101:要打车的FanZzz

    题目链接: http://bjutacm.openjudge.cn/lianxi/1101/ 思路: 二分 + 二分图最大匹配. 开始的时候我想直接用最小费用流模型,后来发现这样是错误的.因为这道题实 ...

  3. Python之pandas数据加载、存储

    Python之pandas数据加载.存储 0. 输入与输出大致可分为三类: 0.1 读取文本文件和其他更好效的磁盘存储格式 2.2 使用数据库中的数据 0.3 利用Web API操作网络资源 1. 读 ...

  4. 02使用常规步骤编译NanoPiM1Plus的Android4.4.2

    02使用常规步骤编译NanoPiM1Plus的Android4.4.2 大文实验室/大文哥 壹捌陆捌零陆捌捌陆捌贰 21504965 AT qq.com 完成时间:2017/12/5 17:51 版本 ...

  5. MySQL——基本安装与使用

    基本安装 下载地址:https://dev.mysql.com/downloads/mysql/ 选择解压版本:mysql-5.7.21-winx64.zip 以管理员身份打开cmd(除了安装服务不要 ...

  6. Python+selenium学习(二) selenium 定位不到元素

    转载:https://www.cnblogs.com/tarrying/p/9681991.html tarrying selenium的三种等待时间 //隐式等待(20秒以内没哥一段时间就会去找元素 ...

  7. c++中的类型转换--reinterpret_cast

    原文链接:  浅析c++中的类型转换--reinterpret_cast转换 reinterpret_cast作用为: 允许将任何指针转换为任何其他指针类型. 也允许将任何整数类型转换为任何指针类型以 ...

  8. CSS 如何让li横向居中显示

    先给一个简单的示例HTML代码 <body> <form id="form1" runat="server"> <div id=& ...

  9. POJ_2387_最短路

    Til the Cows Come Home Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 46859   Accepted ...

  10. react 中样式私有

    解决的问题,两个组件之间  有相同的class名,造成其中一个无法按预期的显示. import React, { Component } from 'react' import styles from ...