链接:




Highways
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 18668   Accepted: 8648

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

POJ Contest,Author:Mathematica@ZSU



题意:

最小生成树的最大权【最小生成树中最长的边】

通:


Kruskal:

Accepted 820K 235MS C++ 1265B

#include<stdio.h>
#include<algorithm>
using namespace std; const int maxn = 500+10; int w[maxn][maxn];
int p[maxn];
int n,m;
int MaxWeight; struct Edge{
int u,v;
int w;
}edge[maxn*maxn/2]; bool cmp(Edge a, Edge b)
{
return a.w < b.w;
} int find(int x)
{
return x == p[x] ? x : p[x] = find(p[x]);
} void Kruskal()
{
for(int i = 1; i <= n; i++) p[i] = i;
sort(edge,edge+m,cmp); for(int i = 0; i < m; i++)
{
int u = find(edge[i].u);
int v = find(edge[i].v); if(u != v)
{
p[v] = u;
MaxWeight = max(MaxWeight, edge[i].w);
}
}
} int main()
{
int T;
scanf("%d", &T);
while(T--)
{
scanf("%d", &n); for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++)
scanf("%d", &w[i][j]); m = 0;
for(int i = 1; i <= n; i++)
{
for(int j = i+1; j <= n; j++)
{
edge[m].u = i;
edge[m].v = j;
edge[m++].w = w[i][j];
}
} MaxWeight = 0;
Kruskal(); printf("%d\n", MaxWeight);
}
return 0;
}

Prime:

Accepted 580K 172MS C++ 943B
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std; const int maxn = 510;
const int INF = 65536*maxn; int w[maxn][maxn];
int d[maxn];
int vis[maxn];
int n;
int MaxWeight; void Prime()
{
for(int i = 1; i <= n; i++) d[i] = INF;
d[1] = 0;
memset(vis, 0, sizeof(vis)); for(int i = 1; i <= n; i++)
{
int x, m = INF;
for(int y = 1; y <= n; y++) if(!vis[y] && d[y] <= m) m = d[x=y];
vis[x] = 1; MaxWeight = max(MaxWeight, d[x]);
for(int y = 1; y <= n; y++) if(!vis[y])
d[y] = min(d[y], w[x][y]);
}
} int main()
{
int T;
scanf("%d", &T);
while(T--)
{
scanf("%d", &n);
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++)
scanf("%d", &w[i][j]); MaxWeight = 0;
Prime();
printf("%d\n", MaxWeight);
}
return 0;
}







POJ 2485 Highways【最小生成树最大权——简单模板】的更多相关文章

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

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

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

                                                                                                         ...

  3. poj 2485 Highways 最小生成树

    点击打开链接 Highways Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19004   Accepted: 8815 ...

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

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

  5. poj 2485 Highways

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

  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. MaterialUp 官方client源代码

    Material Design MaterialUp 官方client源代码  https://github.com/jariz/MaterialUp

  2. jQuery.Validate常用的一些规则

    // 手机号码验证 jQuery.validator.addMethod("mobile", function(value, element) { var length = val ...

  3. 记一起和前端没什么卵关系的OPTION 405问题

    记一起和前端没什么卵关系的后端405问题 问题的关键点在于本来是POST请求,会变成OPTION请求,并且提示405报错,会类似跨域.并且只有某些手机机型才会(如Oppo系列). 其实跨域的问题,如果 ...

  4. [Oracle] - Install Oracle12cR1 on Oracle Linux 6.5 in VirtualBox

    My Oralce Linux 6.5 is running on VirtualBox. Basic settings is 4G memory, 50G hard-disk, auto parti ...

  5. [容器]gcr.io镜像下载

    下载gcr.io的镜像hosts文件  把下面两行加入到/etc/hosts中. 更多在这里http://wst.so/files/hosts 61.91.161.217 gcr.io 61.91.1 ...

  6. Knockout JS 演示样例

    五个小样例,来自Knockout JS官方站点. //tutorial 1 //following codes uses to demonstrate observable values and ta ...

  7. nginx源码学习_源码结构

    nginx的优秀除了体现在程序结构以及代码风格上,nginx的源码组织也同样简洁明了,目录结构层次结构清晰,值得我们去学习.nginx的源码目录与nginx的模块化以及功能的划分是紧密结合,这也使得我 ...

  8. vue实现结账单基本方法

    <script> import axios from 'axios'; export default { name: 'Pos', mounted: function () { var o ...

  9. Crypto++库安装、测试

    项目中需要使用到C++加密解密库,选择了Crypto++这个开源库,于是先安装并写一个小例子试试 一.下载 网址:http://www.cryptopp.com/#download 二.打开项目 下载 ...

  10. python 开发技巧(2)-- Django的安装与使用

    一.安装Django pip3 install django 或者直接使用PyCharm安装 参考 二.添加环境变量 将 "(python安装路径)\Scripts" 添加到环境变 ...