#include<stdlib.h>
#include<stdio.h>
#include<queue> struct vertex//代表一个村庄
{
int minDist;//到相邻结点的最小的距离
bool inMST;//这个村庄是否已经被走过
}; int case_num=0;//用例数
int village_num; //村庄的数目 int edge[500][500];
vertex* vertices=NULL;//指向结构体 村庄 的首地址 int max_lenth=0;//最大的路径 的长度 void init()
{
vertices=(vertex*)malloc(village_num*sizeof(vertex)); for(int i=1;i<village_num;i++)
{
vertices[i].minDist=65536;//都初始化两个村庄之间无限远,也就是没有之间公路
vertices[i].inMST=false;//所有的村庄都没有加入
} vertices[0].minDist=0;//第一个村庄为开始点
vertices[0].inMST=false;
} /*获得未处理的权值最小的顶点*/
int getVertexOfMinDist()
{
int min=65536;
int minIndex=-1;
//遍历所有的点
for(int i=0;i<village_num;i++)
{
//这个点既是未选中点,并且这个点与相邻结点的距离也是与所有结点最小
if(!vertices[i].inMST&&vertices[i].minDist<min){
min=vertices[i].minDist;
minIndex=i;
}
}
return minIndex;
} /*更新和index顶点相邻的顶点的权值*/
void updateMinDist(int index)
{
//遍历所有的点
for(int i=0;i<village_num;i++)
//这个点未选中,并且这个点和所有点距离的最小值大于与目标点(就是距离最小的结点) 之间的距离
if(!vertices[i].inMST&&(vertices[i].minDist>edge[index][i]))
vertices[i].minDist=edge[index][i];
} void prim()
{
vertex* v=NULL;
int minIndex=0; while((minIndex = getVertexOfMinDist()) >= 0)
{
v = vertices + minIndex;
v->inMST = true; if(max_lenth < vertices[minIndex].minDist)
{
max_lenth = vertices[minIndex].minDist;
} updateMinDist(minIndex);
} printf("%d\n",max_lenth);
} void readLine()
{
scanf("%d",&case_num);
for(int i=0;i<case_num;i++)
{
scanf("%d",&village_num);
for(int j=0;j<village_num;j++)
{
for(int k=0;k<village_num;k++)
scanf("%d",&edge[j][k]);
}
} init();
prim();
max_lenth=0;
free(vertices);
} int main()
{
readLine();
return 0;
}

问题描述

Highways

Time Limit: 1000MS   Memory Limit: 65536K
     

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.
 
以下为详细介绍
首先要确定方法:最小生成树
求最小生成树有两种算法另外一个博客里有介绍:https://www.cnblogs.com/lyxcode/p/9176020.html
下面解决这个问题的方法是其中的prim算法
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

北大 ACM highways问题研究(最小生成树)的更多相关文章

  1. 北大 ACM 分类 汇总

    1.搜索 //回溯 2.DP(动态规划) 3.贪心 北大ACM题分类2009-01-27 1 4.图论 //Dijkstra.最小生成树.网络流 5.数论 //解模线性方程 6.计算几何 //凸壳.同 ...

  2. 北大ACM - POJ试题分类(转自EXP)

    北大ACM - POJ试题分类 -- By EXP 2017-12-03 转载请注明出处: by EXP http://exp-blog.com/2018/06/28/pid-38/ 相关推荐文: 旧 ...

  3. 北大ACM题库习题分类与简介(转载)

    在百度文库上找到的,不知是哪位大牛整理的,真的很不错! zz题 目分类 Posted by fishhead at 2007-01-13 12:44:58.0 -------------------- ...

  4. 【POJ 2485】Highways(Prim最小生成树)

    题目 Prim算法:任选一个点,加入集合,找出和它最近的点,加入集合,然后用加入集合的点去更新其它点的最近距离......这题求最小生成树最大的边,于是每次更新一下最大边. #include < ...

  5. ACM第四站————最小生成树(克鲁斯卡尔算法)

    都是生成最小生成树,库鲁斯卡尔算法与普里姆算法的不同之处在于——库鲁斯卡尔算法的思想是以边为主,找权值最小的边生成最小生成树. 主要在于构建边集数组,然后不断寻找最小的边. 同样的题目:最小生成树 题 ...

  6. ACM第四站————最小生成树(普里姆算法)

    对于一个带权的无向连通图,其每个生成树所有边上的权值之和可能不同,我们把所有边上权值之和最小的生成树称为图的最小生成树. 普里姆算法是以其中某一顶点为起点,逐步寻找各个顶点上最小权值的边来构建最小生成 ...

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

    Highways Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Total Sub ...

  8. [ An Ac a Day ^_^ ] [kuangbin带你飞]专题八 生成树 UVA 10600 ACM Contest and Blackout 最小生成树+次小生成树

    题意就是求最小生成树和次小生成树 #include<cstdio> #include<iostream> #include<algorithm> #include& ...

  9. ACM第一天研究懂的AC代码——BFS问题解答——习题zoj2165

    代码参考网址:http://blog.csdn.net/slience_perseverance/article/details/6706354 试题分析: 本题是研究red and black的一个 ...

随机推荐

  1. vue 2.x 的 v-bind 指令的 .prop 事件修饰符详解

    vue 官方文档对 .prop 修饰符的解释是: 使用例子: 那么,具体的原理和用法是什么呢?这要从 html 的 DOM node 说起. 在 html 标签里,我们可以定义各种 attribute ...

  2. mysql基础知识语法汇总整理(二)

    mysql基础知识语法汇总整理(一) insert /*insert*/ insert into 表名(字段列表) values(值列表); --蠕虫复制 (优点:快速复制数据,测试服务器压力) in ...

  3. elasticsearch 单实例安装启动

    elasticsearch 初次启动 下载 elasticsearch-6.3.2.tar.gz 创建目录 /usr/local/elasticsearch/ 解压 tar -zxf elastics ...

  4. springboot的@EnableAutoConfiguration起作用的原理

    通常我们启动一个springboot项目会在启动方法中增加@SpringBootApplicatoin注解,该注解中包含了@EnableAutoConfiguration @Target(Elemen ...

  5. SRS之HLS部署实例源码分析

    1. 综述 SRS 关于 HLS 的具体配置可见: HLS部署实例 SRS 关于 hls 的配置文件内容如下: listen 1935; max_connections 1000; daemon of ...

  6. Ubuntu16 升级nodejs版本

    Ubuntu16下,使用apt-get下载的nodejs最新版本为v4.2.6,而react-native需要v8.x及以上的版本 解决方法在网上找到了这一篇博客Ubuntu安装最新版nodejs,用 ...

  7. MySQL5.7快速修改表中字段长度

    在mysql 5.5版本时,商用环境升级,有一个表存在六千多万数据,升级时需要修改这个表其中一个varchar类型字段的长度,当时用了大概4个多小时,还没有结束,之后我们系统mysql升级到5.7版本 ...

  8. 在Excel中,已知身份证号码,如何批量计算其实际年龄?

    昨天,上司问我:..,你会在Excel中计算年龄吗?当时,一下促住了.说真的,还真不会.今天研究了一下,写下来,方便日后查看. 首先,得有一张已知姓名和相应身份证号的原表吧. 在这张表上再加上三列:出 ...

  9. javascript中ClassName属性的详解与实例

    在javascritp中,我们可以通过style属性可以控制元素的样式,从而实现行为层通过DOM的style属性去干预显示层显示的目标,但是这种方法是不好的,而且为了实现通过DOM脚本设置的样式,你不 ...

  10. Linux Shell 中 > 和 >> 的异同点和应用场景

    Linux Shell 中 > 和 >> 的异同点和应用场景 > 和 >> 的异同点 举例说明(start.sh 为某个服务的启动脚本,start.log 为某服务 ...