链接:




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. lodash escapeRegExp 转义正则特殊字符

    _.escapeRegExp([string='']) 转义RegExp 中特殊的字符 "^", "$", "\", ".&quo ...

  2. 【Excle】Countif函数

    Countif在Excle中是相当的使用,那么我们看下Countif的如下几个功能: ①一对一对比两列数据 ②输入时必须指定包含指定字符 ③帮助Vlookup实现一对多查找 ④统计不重复的个数 一对一 ...

  3. IOS 代理模式 DELEGATE

    代理模式:将我(类或结构体)需要来完成的工作交给另一个具备我所要求的能力的人(实现协议的对象)来执行 协议:具备哪些能力 例子:我要去买火车票,没时间买,委托黄牛买票 协议:买票 //: Playgr ...

  4. SSH——基于datagrid实现分页查询

    1. 修改页面中datagrid的URL地址,访问action // 取派员信息表格 $('#grid').datagrid( { iconCls : 'icon-forward', fit : tr ...

  5. 阿里云OSS服务开通STS安全令牌

    搭建直传服务需要完成以下准备工作: 开通OSS,并且创建Bucket. 开通STS服务. 登录 OSS管理控制台. 在OSS概览页中找到基础配置区域,单击 安全令牌,如下图所示: 进入到 安全令牌快捷 ...

  6. struts上传文件 血案

    记录一个图片上传之后没有后缀 拓展名问题 平常我们查询数据都是  fileImage=fileImageService.getQuery();  让entity等于它 那么fileImage.getF ...

  7. Python绘制分段函数

    1.绘制分段函数:y=4sin(4πt)-sgn(t-0.3)-sgn(0.72-t) import numpy as npimport matplotlib.pyplot as plt#绘制分段函数 ...

  8. C# 读取Ini配置文件类

    配置文件 为fileName.ini 的文件 第一行必须为空,不然读不出值 [section1] key=value key2=value ......... [section2] key=value ...

  9. 收藏 Silverlight中子窗体关闭刷新父窗体(转载)

        public partial class MainPage : UserControl    {        public MainPage()        {            In ...

  10. C++语言基础(4)-构造函数和析构函数

    一.构造函数 类似于java,C++中也有构造函数的概念,相关用法如下: 1.1 构造函数的定义 #include <iostream> using namespace std; clas ...