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. 浅谈iOS内存管理机制

    iOS内存管理机制的原理是引用计数,引用计数简单来说就是统计一块内存的所有权,当这块内存被创建出来的时候,它的引用计数从0增加到1,表示有一个对象或指针持有这块内存,拥有这块内存的所有权,如果这时候有 ...

  2. C# 通过http post 请求上传图片和参数

    一.C# Winform或控制台 /// <summary> /// 通过http上传图片及传参数 /// </summary> /// <param name=&quo ...

  3. 微信小程序wx:for和wx:for-item的正确用法

    wx:for="{{list}}"用来循环数组,而list即为数组名wx:for-item="items" 即用来定义一个循环过程中每个元素的变量的 如果是一维 ...

  4. loadrunner在win10破解提示:Cannot save the license information because acceses to the registry is denied的解决办法

    方法1 下图1-1中提示就是说明了破解的时候权限不足导致,解决办法就是使用管理员权限打开loadrunner破解就好了,但是右键“以管理员身份运行”选项打开loadrunner又是会提示1-2中的问题 ...

  5. 钉钉开发c#帮助类 获取用户信息 DingHelper.cs

    using System;using System.Collections.Generic;using System.Configuration;using System.Linq;using Sys ...

  6. CentOS下Neo4j安装教程

    本文记录一下在CentOS 6.7上,安装neo4j图数据库,本文安装的版本为neo4j-community-2.3.9-unix.tar.gz. 下载Neo4j安装包 使用wget命令获取Neo4j ...

  7. toast components

    toast components https://jossmac.github.io/react-toast-notifications/ https://docs.microsoft.com/en- ...

  8. python自动化之word文档

    #########################docx文件############################ ''' .docx文件有很多结构,有3种不同的类型来表示 在最高一层,Docum ...

  9. redis学习 - 数据持久化

    Redis提供了多种不同级别的持久化方式: RDB 持久化可以在指定的时间间隔内产生数据集的时间点快照(point-in-time snapshot) AOF持久化记录服务器执行的所有写操作命令,并在 ...

  10. 【poj3375】 Network Connection

    http://poj.org/problem?id=3375 (题目链接) 题意 有$M$个网络接口和$N$台计算机,给出它们的坐标(在同一直线上),一个接口只能接一台计算机,费用为两坐标之差的绝对值 ...