#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. zookeeper系列(二)zookeeper的使用--javaAPI

    作者:leesf    掌控之中,才会成功:掌控之外,注定失败: 出处:http://www.cnblogs.com/leesf456/ (尊重原创,感谢作者整理的这么好,作者的部分内容添加了我的理解 ...

  2. Retrofit 使用简介

    一,简介 Retrofit 是目前使用广泛的 Http Client 框架,它适用于 Android 和 Java. 但需要注意的是,Retrofit 本身并不是一个网络请求框架,而是一个网络请求框架 ...

  3. k8s环境部署(一)

    环境介绍 1.单masrer节点 (安装下面图中介绍的四个组件) 2.俩个node节点(安装kubelet和docker) 3.为了支持master与node之前的通信,我们还需要在master上安装 ...

  4. CentOS yum安装软件时保留安装包及依赖包或者自动下载安装包及相关依赖包

    CentOS上安装某个软件一般都有很多相关的依赖包,当然,这也与我们安装时software selection步骤中选择的版本有关系,我们服务器在安装CentOS时一般选择Basic Web Serv ...

  5. 评CSDN上一篇讲述数据迁移的文章“程序员 12 小时惊魂记:凌晨迁移数据出大事故!”

    原文地址:https://blog.csdn.net/csdnnews/article/details/98476886 我的评论:热数据迁移,本不该搞突击,这样一旦出现问题后果不堪设想,多少DBA和 ...

  6. 从浏览器地址栏输入url到显示页面的步骤

      在浏览器地址栏输入URL 浏览器查看缓存,如果请求资源在缓存中并且新鲜,跳转到转码步骤 HTTP1.0提供Expires,值为一个绝对时间表示缓存新鲜日期 HTTP1.1增加了Cache-Cont ...

  7. javascript控制流程语句

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  8. FAQ_1

    FAQ LoadRunner录制脚本时为什么不弹出IE浏览器? 当一台主机上安装多个浏览器时,LoadRunner录制脚本经常遇到不能打开浏览器的情况,可以用下面的方法来解决. 启动浏览器,打开Int ...

  9. [笔记] 使用v2r访问外网

    介绍 首先,你需要有一台能上外网的服务器,如AWS,GCP等. 其次,请自行复制全文,然后将FXXK替换为v2r的全称(5个小写字符). 服务器上Docker镜像配置流程 拉取镜像 $ sudo do ...

  10. Servlet(4):Session

    Session, Cookie及交互 会话(Session)跟踪是Web程序中常用的技术,用来跟踪用户的整个会话.常用的会话跟踪技术是Cookie与Session. Cookie通过在客户端记录信息确 ...