点击打开链接

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. dwr使用步骤

    DWR(Direct Web Remoting)是一个用于改善web页面与Java类交互的远程服务器端Ajax开源框架,可以帮助开发人员开发包含AJAX技术的网站.它可以允许在浏览器里的代码使用运行在 ...

  2. python有序查找算法:二分法

    二分法是一种快速查找的方法,时间复杂度低,逻辑简单易懂,总的来说就是不断的除以2除以2... 例如需要查找有序数组arr里面的某个关键字key的位置,那么首先确认arr的中位数或者中点center,下 ...

  3. phonegap–app启动欢迎引导页localstorage

    对一个新的app,一般情况都会添加一个介绍和欢迎的页面来告诉用户app的功能和新的特性. 那么在phonegap项目里面如何添加这样个引导欢迎页. 这里需要注意的是只有app第一次打开的时候才会有,其 ...

  4. Python基础(二) —— 字符串、列表、字典等常用操作

    一.作用域 对于变量的作用域,执行声明并在内存中存在,该变量就可以在下面的代码中使用. 二.三元运算 result = 值1 if 条件 else 值2 如果条件为真:result = 值1如果条件为 ...

  5. bzoj3035: 导弹防御塔

    Description Freda的城堡——“Freda,城堡外发现了一些入侵者!”“喵...刚刚探究完了城堡建设的方案数,我要歇一会儿嘛lala~”“可是入侵者已经接近城堡了呀!”“别担心,rain ...

  6. android学习笔记14——GridView、ImageSwitcher

    GridView--网格视图.ImageSwitcher--图像切换器 ==> GridView,用于在界面上按行.列的分布形式显示多个组件:GridView和ListView父类相同——Abs ...

  7. Sublime Text 3 自定义配置快捷键

    Settings-User: { "font_face": "Courier New", "font_size": 14.0, " ...

  8. [复变函数]第05堂课 1.4 复球面与 $\infty$; 作业讲解; 2 解析函数 2.1 解析函数的概念与 Cauchy-Riemann 方程

    1. 复球面 大漠孤烟直, 长河落日圆. $$\bex \bbC\cong \bbS^2\bs \sed{N},\quad \bbC_\infty=\bbC\cup \sed{\infty}\mbox ...

  9. 特殊情形的Riemann引理

    设 $f(x)$ 是 $[0,\infty)$ 上的单调函数, 则对任意固定的 $a$, 有 $\dps{\vlm{n}\int_0^a f(x)\sin nx\rd x =0}$; 若同时还有 $\ ...

  10. Expert C# 5.0中的Linq部分

    1.先看看.NET中的Linq 2.扩展方法 3.Lambda表达式和表达式树 4.Linq中的延迟操作 5.Linq中的查询方法 5.1分割操作 5.2连接操作 5.3排序操作 5.4分组和连接 5 ...