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. struts2的运行原理以及底层的工作机制

    1 请求,请求路径是/login(发起请求,被filter拦截) 2 DispatcherFilter 3 获取当前请求的路径 通过request对象 request.getServletPath 4 ...

  2. Alpha阶段敏捷冲刺④

    1.提供当天站立式会议照片一张. 每个人的工作 (有work item 的ID),并将其记录在码云项目管理中: 昨天已完成的工作. 改善界面设计 今天计划完成的工作. 数据库和程序的连接 后端框架的继 ...

  3. CKEditor 4 & markdown & rich text editor

    CKEditor 4 ckeditor 4 http://ckeditor.com

  4. 一张图看懂Function和Object的关系及简述instanceof运算符

    我在写一篇图解prototype和__proto__的区别时,搜资料搜到了一个有意思的现象,下面这两个运算返回的结果是一样的: Function instanceof Object;//true Ob ...

  5. P2261 [CQOI2007]余数求和

    我是题面 题意还是很清晰,很容易理解 1e9范围明显不能暴力,除非你能把常数优化到\(\frac1 {10}\),但我实在想象不到用了这么多取模怎么把常数优化下去 我们可以把\(k\%i\)变成\(k ...

  6. 数据库事物 jdbc事物 spring事物 隔离级别:脏幻不可重复读

    1.数据库事物: 事物的概念 a给b打100块钱的例子 2.jdbc事物: 通过下面代码实现 private Connection conn = null; private PreparedState ...

  7. 1.红黑树和自平衡二叉(查找)树区别 2.红黑树与B树的区别

    1.红黑树和自平衡二叉(查找)树区别 1.红黑树放弃了追求完全平衡,追求大致平衡,在与平衡二叉树的时间复杂度相差不大的情况下,保证每次插入最多只需要三次旋转就能达到平衡,实现起来也更为简单. 2.平衡 ...

  8. Intelligent Factorial Factorization LightOJ - 1035(水题)

    就是暴力嘛...很水的一个题... 不好意思交都... #include <iostream> #include <cstdio> #include <sstream&g ...

  9. NOIP2017 游记

    没考多好......并不知道该写什么...... 那就写写流水账...... DAY 0 上午到机房,众人全是打板子or颓废的....然后我打完板子去打印了个奇怪的背包九讲.... 然后大巴到德州东, ...

  10. 查看ubuntu版本号

    No.1 cat /etc/issue No.2 cat /proc/version No.3 uname -a No.4 lsb_release -a