1090. Highways

Constraints

Time Limit: 1 secs, Memory Limit: 32 MB

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 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.

Output

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.
This problem contains multiple test cases!
The first line of a multiple input is an integer T, then a blank line followed by T input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.
The output format consists of T output blocks. There is a blank line between output blocks.

Sample Input

1

3
0 990 692
990 0 179
692 179 0

Sample Output

692

使用kruskal算法,注意并查集技术

#include <iostream>
#include <set>
#include <vector>
#include <algorithm>
using namespace std; int dis[501][501];
struct Edge
{
int a;
int b;
int w;
Edge(int aa,int bb,int ww)
{
a = aa;
b = bb;
w = ww;
}
}; bool cmp(Edge aa,Edge bb)
{
return aa.w < bb.w;
} int a[501]; //使用并查集实现kruskal算法
int find(int x)
{
if(x == a[x])
return x;
else
{
a[x] = find(a[x]);
return a[x];
}
} int main()
{
int T;
cin >> T;
while(T--)
{
set<int> ss;
vector<Edge> tt;
int N;
cin >> N;
int i,j;
for(i = 1;i <= N;i++)
{
for(j = 1;j <= N;j++)
{
cin >> dis[i][j];
if(i > j)
tt.push_back(Edge(i,j,dis[i][j]));
}
}
sort(tt.begin(),tt.end(),cmp);
int max = 0;
int num = 0;
for(i = 1;i <= N;i++)
a[i] = i;
for(i = 0;i < tt.size();i++)
{
if(find(tt[i].a) != find(tt[i].b))
{
num++;
max = tt[i].w;
a[find(tt[i].a)] = a[find(tt[i].b)];
}
if(num == N-1)
break;
}
cout << max << endl;
if(T != 0)
cout << endl;
}
return 0;
}

soj1090.Highways的更多相关文章

  1. H:Highways

    总时间限制: 1000ms 内存限制: 65536kB描述The island nation of Flatopia is perfectly flat. Unfortunately, Flatopi ...

  2. Highways(prim & MST)

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

  3. poj2485 Highways

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

  4. poj 2485 Highways 最小生成树

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

  5. poj 2485 Highways

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

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

    Highways Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit Sta ...

  7. POJ 1751 Highways (最小生成树)

    Highways 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/G Description The island nation ...

  8. UVa 1393 (容斥原理、GCD) Highways

    题意: 给出一个n行m列的点阵,求共有多少条非水平非竖直线至少经过其中两点. 分析: 首先说紫书上的思路,编程较简单且容易理解.由于对称性,所以只统计“\”这种线型的,最后乘2即是答案. 枚举斜线包围 ...

  9. (poj) 1751 Highways

    Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has a very poor ...

随机推荐

  1. 解决nginx+uWSGI部署Django时遇到的static文件404的问题

    昨天是利用Django自带的runserver部署的服务器,但是由于runserver比较不稳定,因此决定采用uWSGI+nginx进行部署. 昨天已经安装好了uwsgi和nginx,使用该指令打开8 ...

  2. 关于Actions和Robot的区别简单说明

    Actions和Robot都是可以用来模拟键盘操作,但是两者还是有区别的 Actions actions =new Actions(driver) 实例化一个Actions后,我们可以使用action ...

  3. 将博客搬至CSDN和和自己的网站

    将博客同步一份到CSDN去, CSDN博客地址:https://blog.csdn.net/klkfl ---------------- 分割线 2018-10-7 自己用typecho 搭建了一个博 ...

  4. 工作中常用到的Linux命令

    ps: (ps的参数分成basic, list, output, thread, miscellaneous) (basic) -e / -A 显示所有进程 (output) -o 输出指定字段 ls ...

  5. Nmap用法实例

    <给Linux系统/网络管理员的nmap的29个实用例子> https://linux.cn/article-2561-1.html

  6. 一个java高级工程师的进阶

    宏观方面 一. JAVA.要想成为JAVA(高级)工程师肯定要学习JAVA.一般的程序员或许只需知道一些JAVA的语法结构就可以应付了.但要成为JAVA(高级) 工程师,您要对JAVA做比较深入的研究 ...

  7. java中的==操作符和equals函数

    基本规则 “==”操作符的使用需要分成两种情况 判值类型相等 这一点很好理解,两个值类型代表的数值相等,则“==”表达式返回true “==”可以用与不同值类型的比较,语言会自动进行类型转换 判引用类 ...

  8. DAY5-Flask项目

    1.验证参数(WTForms): 当URL为/book/search?q= &page=1 时 ,p=空格,验证器会通过,在forms验证层的book.py文件中添加DataRequired验 ...

  9. BZOJ5101 POI2018Powódź(并查集)

    如果某个格子的积水量超过了该格子的某个挡板高度,那么挡板另一端的积水量就会与其相同.看起来是一个不断合并的过程,考虑并查集.枚举深度,维护每个连通块内的方案数,深度超过某挡板高度时,将两端的连通块合并 ...

  10. 自平衡二叉(查找树/搜索树/排序树) binary search tree

    在计算机科学中,AVL树是最先发明的自平衡二叉查找树.AVL树得名于它的发明者 G.M. Adelson-Velsky 和 E.M. Landis,他们在 1962 年的论文 "An alg ...