sicily 1090. Highways
2015-10-30 20:48
411 查看
1090. Highways
Constraints
Time Limit: 1 secs, Memory Limit: 32 MBDescription
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 possibleto 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 villagei 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
题目分析
最小生成树的最长边
每两个案例间输出空行
#include <iostream>
#include <algorithm>
#include <memory.h>
struct Path {
int len;
int start, end;
};
bool com(Path a, Path b) {
return a.len < b.len;
}
int main()
{
int test;
std::cin >> test;
bool first = true;
while (test--) {
if (first) first = false;
else std::cout << std::endl;
int num;
std::cin >> num;
int root[num];
for (int i = 0; i < num; ++i)
root[i] = i;
Path paths[10000];
int count = 0;
int temp;
for (int i = 0; i < num; ++i) {
for (int j = 0; j < num; ++j) {
std::cin >> temp;
if (i < j) {
paths[count].start = i; paths[count].end = j; paths[count].len = temp;
++count;
}
}
}
std::sort(paths, paths+count, com);
int numpath = 0;
int index = 0;
while (true) {
if (root[paths[index].start] != root[paths[index].end]) {
temp = root[paths[index].end];
for (int i = 0; i < num; ++i)
if (root[i] == temp)
root[i] = root[paths[index].start];
++numpath;
if (numpath == num-1) {
index = paths[index].len;
break;
}
}
++index;
}
std::cout << index << std::endl;
}
}
相关文章推荐
- BeautifulSoup的使用学习笔记
- Xcode 7.1项目中Objective-C和swift混合编程(一)
- hdu 1254 推箱子(嵌套搜索)
- ios响应者链
- Windows 远程连接Hbase常见错误
- Leetcode-Remove Duplicates from Sorted List
- BZOJ4300 绝世好题
- 一个有序正整数集S,若要保证整数集中所有的数通过组合(相加)可以表示1~N中任意一个正整数
- C++编写安全OCX,IE不弹出安全提示
- 腾讯新闻评论数据爬取
- python模块官网大全
- 二维数组中的查找
- 软工文档中的那些图
- sicily 1031. Campus
- Java基础学习12(Java SE图解以及Java基本思想讲解)
- Java 自动装箱与拆箱(Autoboxing and unboxing)
- 排序
- HANA学院(2)-使用数据服务(use data services)
- IC设计笔试
- poj3461Oulipo