#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. leetcode题目10.正则表达式匹配(困难)

    题目描述: 给你一个字符串 s 和一个字符规律 p,请你来实现一个支持 '.' 和 '*' 的正则表达式匹配. '.' 匹配任意单个字符'*' 匹配零个或多个前面的那一个元素所谓匹配,是要涵盖 整个  ...

  2. LeetCode 36. 有效的数独(Valid Sudoku)

    题目描述 判断一个 9x9 的数独是否有效.只需要根据以下规则,验证已经填入的数字是否有效即可. 数字 1-9 在每一行只能出现一次. 数字 1-9 在每一列只能出现一次. 数字 1-9 在每一个以粗 ...

  3. Walkthrough: My first WPF desktop application

    Walkthrough: My first WPF desktop application This article shows you how to develop a Windows Presen ...

  4. leetcode-hard-array-238. Product of Array Except Self-NO

    mycode   99.47% class Solution(object): def productExceptSelf(self, nums): """ :type ...

  5. js获取当前日期并格式yyy-MM-dd

    //格式化日期:yyyy-MM-dd function formatDate(date) { var myyear = date.getFullYear(); var mymonth = date.g ...

  6. AXIS 1.4 自定义序列化/反序列化类

    axis1.4的CalendarDeserializer 使用的时区是GMT,导致日期显示不准确 private static SimpleDateFormat zulu = new SimpleDa ...

  7. Oracle 设置主键自增长

    如果想在Oracle数据库里实现数据表主键自增,我们似乎没有办法像MySql般直接定义列的属性来实现.不过对于这个数据库的常用功能,我们还是有办法实现的.这里将展示使用触发器来实现主键自增. 1.准备 ...

  8. 点击其他区域关闭dialog

    1.在打开dialog处阻止冒泡,在body click事件中关闭dialog2.不阻止冒泡,在body click事件中判断target是否为diallog或其子节点 在Safari浏览器中,在默认 ...

  9. Linux 服务器基本优化

    一:修改ulimit数 vi /etc/security/limits.conf 添加如下行: * soft noproc 65535 * hard noproc 65535 * soft nofil ...

  10. 日期格式存入字符类型之后,再转回datetime类型报错

    背景 最近我们迁移了一批服务器,因我们在azure portal 上新开的VM暂时默认只有英文系统,所以我们在开设好的数据库服务器的时候,都会重置数据库字符排序成中文,避免出现中文乱码问题,重置参考路 ...