点击打开链接

Highways
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 19004   Accepted: 8815

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.

题目大意是要在多个村庄之间修公路,给出公路之间的距离,求总花费最小的方案中费用最高的那条公路,很裸的最小生成树问题

这题我用了prim + priority_queue实现的

#include<stdio.h>
#include<string.h>
#include<queue>
#include<utility>
using namespace std;
int map[501][501];
int n; int prime()
{
bool flag[501] = {0};
priority_queue< pair<int, int> , vector<pair<int, int > > , greater<pair<int ,int > > > q;
pair<int, int > p, new_p;
p.first = 0;
p.second = 1;
q.push(p);
int max = 0;
int t = 1;
int j;
for(j = 1; j <= n ;j++)
{
p = q.top();
q.pop();
if(flag[p.second] == 1)
{
j--;
continue;
}
if(max < p.first)
max = p.first;
flag[p.second] = 1;
int i;
for(i = 1 ; i <= n; i++)
{
if(map[p.second][i] != 0 && flag[i] == 0)
{
new_p.first = map[p.second][i];
new_p.second = i;
q.push(new_p);
}
}
}
return max;
}
int main()
{
int t;
scanf("%d", &t);
while(t--)
{
memset(map, 0, sizeof(map));
scanf("%d", &n);
int i, j;
for(i = 1; i <= n; i++)
{
for(j = 1; j <= n; j++)
{
scanf("%d", &map[i][j]);
}
}
int f = prime();
printf("%d\n", f);
} return 0;
}

poj 2485 Highways 最小生成树的更多相关文章

  1. POJ 2485 Highways(最小生成树+ 输出该最小生成树里的最长的边权)

                                                                                                         ...

  2. POJ 2485 Highways 最小生成树 (Kruskal)

    Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public h ...

  3. poj 2485 Highways (最小生成树)

    链接:poj 2485 题意:输入n个城镇相互之间的距离,输出将n个城镇连通费用最小的方案中修的最长的路的长度 这个也是最小生成树的题,仅仅只是要求的不是最小价值,而是最小生成树中的最大权值.仅仅须要 ...

  4. poj 2485 Highways

    题目连接 http://poj.org/problem?id=2485 Highways Description The island nation of Flatopia is perfectly ...

  5. POJ 2485 Highways【最小生成树最大权——简单模板】

    链接: http://poj.org/problem?id=2485 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...

  6. POJ 2485 Highways( 最小生成树)

    题目链接 Description The islandnation of Flatopia is perfectly flat. Unfortunately, Flatopia has no publ ...

  7. POJ 2485 Highways (求最小生成树中最大的边)

    Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public h ...

  8. POJ 2485 Highways (prim最小生成树)

    对于终于生成的最小生成树中最长边所连接的两点来说 不存在更短的边使得该两点以不论什么方式联通 对于本题来说 最小生成树中的最长边的边长就是使整个图联通的最长边的边长 由此可知仅仅要对给出城市所抽象出的 ...

  9. poj 2485 Highways(最小生成树,基础,最大边权)

    题目 //听说听木看懂之后,数据很水,我看看能不能水过 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stri ...

随机推荐

  1. 【转】3篇:Xilium CefGlue 关于 CLR Object 与 JS 交互类库封装报告:官方原生方法分析

    作者: 牛A与牛C之间 时间: 2013-11-17 分类: 技术文章 | 暂无评论 | 编辑文章 主页 » 技术文章 » 第3篇:Xilium CefGlue 关于 CLR Object 与 JS ...

  2. ant脚本编写

    使用ant脚本前的准备 1.下载一个ant安装包.如:apache-ant-1.8.4-bin.zip.解压到E盘. 2.配置环境变量.新增ANT_HOME:E:\apache-ant-1.8.4:P ...

  3. mootools upgrate from 1.2 to 1.3 or 1.4

    Update from 1.2 to 1.3 lorenzos edited this page on 8 Jul 2011 · 2 revisions Pages 19 Home Home Chan ...

  4. Java 读Properties

    import java.io.*; import java.util.Properties; public class Study { public static void main(String[] ...

  5. MySQL时间戳和时间格式转换函数

    MySQL时间戳和时间格式转换函数:unix_timestamp and from_unixtime unix_timestamp将时间转化成时间戳格式.from_unixtime将时间戳转化成时间格 ...

  6. android学习笔记48——SQLite

    SQLite SQLite试试一个嵌入式的数据库引擎,专门用于资源有限的设备(如手机.PDA)上适量数据存取. SQLite支持绝大部分SQL92语法,同样允许开发者使用SQL语句操作数据库中的数据, ...

  7. item31,连续子数组的最大和

    整型数组,元素有正数和负数.数组中一个或连续的多个整数组成一个子数组,求所有子数组中最大值. =========== 动态规划, 状态转移方程,max[].size = nums.size() max ...

  8. 【转】SQL SERVER 存储过程中变量的作用域

    今天遇到一个很有趣的事情,以前没有注意过,所以记下来. 先来看例子. SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE PROCEDURE ...

  9. ADO.NET(查询、属性扩展)

    一.ADO.NET 融合面向对象的查询语句 1.只查询一条数据 //数据访问中的select方法 public stu select(string xuehao) { stu s = null; cm ...

  10. 关于this指向思考

    事情起因为<js函数式编程指南>里面看到这段话: 上例DB对象中的save方法作为参数传入到fs的readFile,按道理this指向DB,为啥作者的意思是存在this指针可能会指向其他地 ...