soj1090.Highways
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的更多相关文章
- H:Highways
总时间限制: 1000ms 内存限制: 65536kB描述The island nation of Flatopia is perfectly flat. Unfortunately, Flatopi ...
- Highways(prim & MST)
Highways Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 23421 Accepted: 10826 Descri ...
- poj2485 Highways
Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public h ...
- poj 2485 Highways 最小生成树
点击打开链接 Highways Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19004 Accepted: 8815 ...
- poj 2485 Highways
题目连接 http://poj.org/problem?id=2485 Highways Description The island nation of Flatopia is perfectly ...
- POJ 1751 Highways (最小生成树)
Highways Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Submit Sta ...
- POJ 1751 Highways (最小生成树)
Highways 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/G Description The island nation ...
- UVa 1393 (容斥原理、GCD) Highways
题意: 给出一个n行m列的点阵,求共有多少条非水平非竖直线至少经过其中两点. 分析: 首先说紫书上的思路,编程较简单且容易理解.由于对称性,所以只统计“\”这种线型的,最后乘2即是答案. 枚举斜线包围 ...
- (poj) 1751 Highways
Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has a very poor ...
随机推荐
- C语言中Union类型的使用方法
转自:http://blog.csdn.net/feimor/article/details/6858103 使用C语言时,常常使用struct,对于union类型却几乎没有用过,只知道它是联合类型, ...
- 2D变换
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- php错误报告和调试
3. 错误报告和调试 常常犯的一个错误是忘记关闭 PHP 错误和数据库错误报告,这样做是有风险的.在任何一个公开的站点,error_reporting 应该设置为0 ,最多只能设置为 E_ERROR, ...
- 使用WinSW 将 exe 创建成Windows下面 service的方法 (将nginx创建成 services)
1. 使用winsw工具能够将部分exe 创建成服务, 这样可以很简单的创建nginx的服务, 避免每次需要执行相应的命令. 2. 方法,下载 工具 地址 github https://github. ...
- 在动作类上加上SkipValidation 在反射时候会获取到该反射信息 就不会执行validate方法
在动作类上加上SkipValidation 在反射时候会获取到该反射信息 就不会执行validate方法
- ios开发之 -- xib关联自定义view
在xib下使用自定义的view,因为很多时候,可能幸亏自顶一个view,然后在view里面填充控件,但是需要重写很多无用的 代码,而且很容易出错不说,还很好工作量,使用xib的话,分钟搞定一个view ...
- bzoj2302-Problem c
题意 有 \(n\) 个人,从 1 到 \(i\) 编号.给每个人一个值 \(a_i\) ,他们会按编号从小到大进行如下操作:查看 \(a_i\) 有没有人,若没有就坐进去,否则查看 \(a_i+1\ ...
- Linux下安装 mongodb
1.下载 去官网 https://www.mongodb.com 找到对应版本的mongodb 的下载地址 这样的: https://fastdl.mongodb.org/linux/mongodb ...
- ansible部署(pip安装)
centos7 pip安装 ansible 首先ansible基于python2.X 环境 默认centos都已经安装好了python2环境 安装可选性 ansible可以通过源码,yum,pip等方 ...
- BZOJ1064 NOI2008假面舞会(dfs树)
将图中的环的长度定义为正向边数量-反向边数量,那么答案一定是所有环的环长的共同因子.dfs一下就能找到图中的一些环,并且图中的所有环的环长都可以由这些环长加加减减得到(好像不太会证).如果有环长为1或 ...