北大 ACM highways问题研究(最小生成树)
#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
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 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
Sample Input
1 3
0 990 692
990 0 179
692 179 0
Sample Output
692
Hint
北大 ACM highways问题研究(最小生成树)的更多相关文章
- 北大 ACM 分类 汇总
1.搜索 //回溯 2.DP(动态规划) 3.贪心 北大ACM题分类2009-01-27 1 4.图论 //Dijkstra.最小生成树.网络流 5.数论 //解模线性方程 6.计算几何 //凸壳.同 ...
- 北大ACM - POJ试题分类(转自EXP)
北大ACM - POJ试题分类 -- By EXP 2017-12-03 转载请注明出处: by EXP http://exp-blog.com/2018/06/28/pid-38/ 相关推荐文: 旧 ...
- 北大ACM题库习题分类与简介(转载)
在百度文库上找到的,不知是哪位大牛整理的,真的很不错! zz题 目分类 Posted by fishhead at 2007-01-13 12:44:58.0 -------------------- ...
- 【POJ 2485】Highways(Prim最小生成树)
题目 Prim算法:任选一个点,加入集合,找出和它最近的点,加入集合,然后用加入集合的点去更新其它点的最近距离......这题求最小生成树最大的边,于是每次更新一下最大边. #include < ...
- ACM第四站————最小生成树(克鲁斯卡尔算法)
都是生成最小生成树,库鲁斯卡尔算法与普里姆算法的不同之处在于——库鲁斯卡尔算法的思想是以边为主,找权值最小的边生成最小生成树. 主要在于构建边集数组,然后不断寻找最小的边. 同样的题目:最小生成树 题 ...
- ACM第四站————最小生成树(普里姆算法)
对于一个带权的无向连通图,其每个生成树所有边上的权值之和可能不同,我们把所有边上权值之和最小的生成树称为图的最小生成树. 普里姆算法是以其中某一顶点为起点,逐步寻找各个顶点上最小权值的边来构建最小生成 ...
- Highways(求最小生成树的最大边)
Highways Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) Total Sub ...
- [ An Ac a Day ^_^ ] [kuangbin带你飞]专题八 生成树 UVA 10600 ACM Contest and Blackout 最小生成树+次小生成树
题意就是求最小生成树和次小生成树 #include<cstdio> #include<iostream> #include<algorithm> #include& ...
- ACM第一天研究懂的AC代码——BFS问题解答——习题zoj2165
代码参考网址:http://blog.csdn.net/slience_perseverance/article/details/6706354 试题分析: 本题是研究red and black的一个 ...
随机推荐
- Java并发编程的艺术笔记(十)——Semaphore详解
作用:控制同时访问某个特定资源的线程数量,用在流量控制.
- Java多线程-程序运行堆栈分析
class文件内容 class文件包含JAVA程序执行的字节码:数据严格按照格式紧凑排列在class文件中的二进制流,中间无任何分隔符:文件开头有一个0xcafebabe(16进制)特殊的一个标志. ...
- DP&图论 DAY 4 下午图论
DP&图论 DAY 4 下午 后天考试不考二分图,双联通 考拓扑排序 图论 图的基本模型 边: 有向边构成有向图 无向边构成无向图 权值: 1.无权 2.点权 3.边权 4.负权(dij不 ...
- 本地oracle可以通过localhost连接,无法通过ip地址连接解决方法,oracle远程连接配置
Oracle11g安装后只有本地可以连接,远程无法连接,而且本地只能配置成localhost配置成IP地址也无法连接. 这是因为安装oracle的时候没有配置远程的监听,默认的监听是localhost ...
- javascript 生成img标签的3种方式(对象、方法、html)
<div id="d1"></div> <script> //HTML function a(){ document.getElementByI ...
- openerp学习笔记 计划动作、计划执行(维护计划)
示例代码: data/scheduler.xml <?xml version="1.0" encoding="utf-8"?><openerp ...
- 远程管理控制ssh
传统的网络服务程序,FTP.POP.telnet 本质上都是不安全的,因为它们在网络上通过明文传送口令和数据,这些数据非常容易被截获.SSH叫做Secure Shell.通过SSH,可以把传输数据进行 ...
- 深入学习重点分析java基础---第一章:深入理解jvm(java虚拟机) 第一节 java内存模型及gc策略
身为一个java程序员如果只会使用而不知原理称其为初级java程序员,知晓原理而升中级.融会贯通则为高级 作为有一个有技术追求的人,应当利用业余时间及零碎时间了解原理 近期在看深入理解java虚拟机 ...
- Could not resolve host: mirrorlist.centos.org Centos 7 Unkown error
安装Centos7(core)以后,网卡默认不会启用.这是一个大坑,直接报错,这是一个过度优化,有几个开发人员/运维人员安装centos7(core)不用ssh去连接服务器的. 报错如下: Loade ...
- JavaScript基础入门06
目录 JavaScript 基础入门06 Math 对象 Math对象的静态属性 Math对象的静态方法 指定范围的随机数 返回随机字符 三角函数 Date对象 基础知识 日期对象具体API 构造函数 ...