博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ 2485 Highways(最小生成树Prim算法)
阅读量:4496 次
发布时间:2019-06-08

本文共 3010 字,大约阅读时间需要 10 分钟。

Highways

Time Limit: 1000MS

 

Memory Limit: 65536K

Total Submissions: 14534

 

Accepted: 6776

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 of input is an integer T, which tells how many test cases followed. 

The first line of each case 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. There is an empty line after each test case.

Output

For each test case, 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.

Sample Input

1

 

3

0 990 692

990 0 179

692 179 0

Sample Output

692

Hint

Huge input,scanf is recommended.

Source

,Author:Mathematica@ZSU

 解题报告:这道题就是求最小生成树的同时,求出最小生成树中最长的一个边;

代码如下:

#include 
#include
#include
using namespace std; const int N = 1010; const int INF = 0x7fffffff; int map[N][N], dis[N], visit[N]; int n, ans; void Prim() {
int i, j, p, min; memset(visit, 0, sizeof(visit)); memset(dis, 0, sizeof(dis)); for(i = 1; i <= n; ++i) {
dis[i] = map[1][i]; } visit[1] = 1; for (i = 1; i <= n; ++i) {
min = INF; for (j = 1; j <= n; ++j) {
if (!visit[j] && min > dis[j]) {
min = dis[j]; p = j; } } visit[p] = 1; if (ans < dis[p])//求最长的一条边 {
ans = dis[p]; } for (j = 1; j <= n; ++j) {
if (!visit[j] && dis[j] > map[p][j]) {
dis[j] = map[p][j]; } } } } int main() {
int i, t, j; scanf("%d", &t); while (t --) {
scanf("%d", &n); memset(map, 0, sizeof(map)); for (i = 1; i <= n; ++i) {
for (j = 1; j <= n; ++j) {
scanf("%d", &map[i][j]); } } ans = -1;//哎一开始的时候忘了赋初值,WA了一次 Prim(); printf("%d\n", ans); } }

转载于:https://www.cnblogs.com/lidaojian/archive/2012/03/09/2388301.html

你可能感兴趣的文章
druid监控配置
查看>>
css颜色代码大全
查看>>
物理系统(二)
查看>>
css3中-moz、-ms、-webkit与盒子模型
查看>>
DataTable 整行为空时,去除空行,常用于Excel导入,转换为DataTable时出现
查看>>
网络相关面试题1
查看>>
一种让谷歌搜索引擎拒绝搜索的字符串
查看>>
实现毛玻璃效果
查看>>
[BZOJ4082][Wf2014]Surveillance[倍增]
查看>>
kill -9杀掉nginx主进程、reload失败解决办法
查看>>
objdump 用法
查看>>
前端js模糊搜索(模糊查询)
查看>>
Chrome的hack写法以及CSS的支持程度图示
查看>>
苹果端手机微信页面长按图片无法保存的解决方案
查看>>
SVN、GIT比较
查看>>
asp后台读id设置样式
查看>>
POJ 3744 Scout YYF I 概率DP+矩阵快速幂 好题
查看>>
Training Logisches Denken
查看>>
谁分配谁释放
查看>>
正则表达式
查看>>