#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. 8.4 JavaScript(一)

    参考链接:http://how2j.cn/k/javascript/javascript-javascript-tutorial/519.html 一.JavaScript是什么 JavaScript ...

  2. badboy——jmeter录制工具

    web网站录制工具 输入网址:红点点被选中代表在录制,然后点点点: 然后导出: 在从JMETER打开:(注意,一定要填cookie)

  3. 用 Docker 搭建 ORACLE 数据库开发环境

    用 Docker 搭建 ORACLE 数据库开发环境 需要安装 ORACLE 数据库做开发,直接安装的话因为各类平台的限制,非常复杂,会遇到很多问题. 还好,现在有 Docker 化的部署方式,省去很 ...

  4. js 处理url参数,应用导航分类

    1.先上图 2.代码 html <li><a href="javascript:void(0);" data-cid = "{$v['id']}&quo ...

  5. 最简SpringBoot程序制法

    JDK:1.8.0_212 IDE:STS4(Spring Tool Suit4 Version: 4.3.2.RELEASE) 工程下载:https://files.cnblogs.com/file ...

  6. zipkin-client:brave核心代码思路整理

    Zipkin是分布式跟踪系统. 简单地理解,可以将Zipkin分为两部分. 一部分为Zipkin Server,其负责接受存储应用程序处理耗时数据,以及UI展示. 另一部分为Zipkin Client ...

  7. linuxs上mono

    当前,在Linux系统上架设ASP.NET网站.建设WEB应用工程项目已经在国内流行起来,而“Mono+Jexus”架构模式是Linux承载ASP.NET企业级应用的极为重要的架构方式,这种架构中,J ...

  8. sptringboot2.0实现aop

    题记:在项目需要对请求日志情形管理. 声明:参考博客https://blog.csdn.net/bombsklk/article/details/79143145 1.在pom.xml中加入依赖 &l ...

  9. Javascript 二维码生成库:QRCode

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

  10. SQLite 版本引发的 Python 程序调用问题

    问题 在跑 OpenStack functional 功能测试的时候有两个用例过不去. nova.tests.functional.db.test_resource_provider.Resource ...