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 ...
随机推荐
- python安装报错:Microsoft Visual C++ 14.0 is required
保存详情如下: error: Microsoft Visual C++ 14.0 is required. Get it with "Microsoft Visual C++ Build T ...
- LinkedList,HashSet,HashMap
LinkedList底层源码是采用双向链表实现的 private static class Node<E> { E item;//节点值 Node<E> next;//节点后指 ...
- Python语言算法的时间复杂度和空间复杂度
算法复杂度分为时间复杂度和空间复杂度. 其作用: 时间复杂度是指执行算法所需要的计算工作量: 而空间复杂度是指执行这个算法所需要的内存空间. (算法的复杂性体现在运行该算法时的计算机所需资源的多少上, ...
- Netty基础系列(3) --彻底理解NIO
前言 上一节中我们提到了同步异步与阻塞非阻塞的区别,知道了同步并不等于阻塞.而本节的主角NIO是一种同步非阻塞的I/O模型,并且是I/O多路复用模型.NIO在java中被称为 New I/O.它并不能 ...
- CIR,CBS,EBS,PIR,PBS傻傻分不清楚?看这里!—-揭秘令牌桶
概述 春暖花开的时候,大家都开着汽车外出旅游欣赏美丽的风景,却被堵在高速公路上,你是否为此感到痛苦?但如果有一种机制可以评估高速公路上的车流量.控制车流情况,确保进入高速公路的汽车都能在路上安全畅行, ...
- What Kind of Friends Are You? ZOJ - 3960(ZheJiang Province Contest)
怎么说呢...我能说我又过了一道水题? emm... 问题描述: 给定 n 个待确定名字的 Friends 和 q 个问题.已知 c 个 Friends 的名字. 对于第 i 个问题,有 个 Fri ...
- variant conversion error for variable v23
excel数据导入到oracle数据库出现的问题 V23指的是excel列.,这列的数据长度超出或者类型与数据库表不一致导致的 解决方法,1.清空该列,再建个新列 2.用access SQL查出长度 ...
- MT【137】多少个?
数列\(\{a_n\}\)共11项,\(a_1=0,a_{11}=4\),且\(|a_{k+1}-a_{k}|=2,k=1,2,\cdots,10\) 求满足条件的不同的数列的个数______ 解答: ...
- 【刷题】洛谷 P3573 [POI2014]RAJ-Rally
题目描述 An annual bicycle rally will soon begin in Byteburg. The bikers of Byteburg are natural long di ...
- [SDOI2013]项链
description luogu 最近,铭铭迷恋上了一种项链.与其他珍珠项链基本上相同,不过这种项链的珠子却与众不同,是正三菱柱的泰山石雕刻而成的. 三菱柱的侧面是正方形构成的,上面刻有数字. 能够 ...