poj2485 highwaysC语言编写
Time Limit: 1000MS
Memory Limit: 65536K
Total Submissions: 33595
Accepted: 15194
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*/
#include<stdio.h>
#define v 501 //顶点的最大数目
#define max 66666 //用于比较的数字,比max 大视为不相邻
int map[v][v];//存储顶点的关系
void prim(int n)
{int i,j;
int min;//距离生成树最近的距离
int way[v];//存放各顶点到树的距离
int r=0;//每次放入树中顶点的标号
int p[v];//判断顶点是否在树里,并存放顶点标号
int ans=0;//存放最小生成树里的最大权值(两顶点之间最大距离)
for(i=0;i<n;i++) p[i]=-1;//不在树里记为-1‘在记为1
p[0]=0;//开始前把0顶点放在树里
for(i=0;i<n;i++)
way[i]=map[i][r];//其余顶点到--树--的距离
for(i=1;i<n;i++)//判断n-1次 way[n]
{min=max;
for(j=0;j<n;j++)//在可以比较的距离里面选择最近的标号出来
if(way[j] && way[j]<min)
{ min=way[j]; r=j; }
p[r]=r;//选中的顶点放到数组里
way[r]=0;//该顶点在树里故距离变成0
if(ans<min) ans=min;//更新ans的值
for(j=0;j<n;j++)//更新way
{
if(way[j] && map[j][r]<way[j])
way[j]=map[j][r];
}
}
printf("%d\n",ans);
}
int main()
{
int i,j,t,n;
//t是多少组数据。n是多少个城市
scanf("%d", &t);//输入T组数据
while(t--)
{
scanf("%d",&n);//n 个城市
for(i=0;i<n;i++) //输入城市间的距离
for(j=0;j<n;j++)
scanf("%d",&map[i][j]);
prim(n) ;
}
return 0;
}
poj2485 highwaysC语言编写的更多相关文章
- 基于php基础语言编写的小程序之计算器
基于php基础语言编写的小程序之计算器 需求:在输入框中输入数字进行加.减.乘.除运算(html+php) 思路: 1首先要创建输入数字和运算符的输入框,数字用input的text属性,运算符用sel ...
- 如何用C语言编写病毒‘
怎样用C语言编写病毒在分析病毒机理的基础上,用C语言写了一个小病毒作为实例,用TURBOC2.0实现.[Abstract] This paper introduce the charateristic ...
- 选择使用c语言编写的phalcon框架
使用这个框架,我总结了如下几点考虑 1.这个框架速度快.纯c语言编写的框架,速度都比php框架快,省去了中间环节.当然,使用它不仅仅是性能考虑.因为如果为了解决php性能问题,完全可以有很多种方式,不 ...
- [改善Java代码]易变业务使用脚本语言编写
建议16: 易变业务使用脚本语言编写 Java世界一直在遭受着异种语言的入侵,比如PHP.Ruby.Groovy.JavaScript等,这些“入侵者”都有一个共同特征:全是同一类语言—脚本语言,它们 ...
- 运用Python语言编写获取Linux基本系统信息(三):Python与数据库编程,把获取的信息存入数据库
运用Python语言编写获取Linux基本系统信息(三):Python与数据库编程 有关前两篇的链接: 运用Python语言编写获取Linux基本系统信息(一):获得Linux版本.内核.当前时间 运 ...
- 运用Python语言编写获取Linux基本系统信息(二):文件系统使用情况获取
本文跟着上一篇文章继续写,上一篇文章的链接 运用Python语言编写获取Linux基本系统信息(一):获得Linux版本.内核.当前时间 一.随便说说 获取文件系统使用情况的思路和上一篇获取主要系统是 ...
- C语言编写的bmp读写程序
C语言编写的bmp读写程序 建议先把bmp的数据存储格式了解下 <span style="font-size:16px;">#include "Windows ...
- 使用C语言编写windows服务一般框架
原文:使用C语言编写windows服务一般框架 编写windows服务和编写windows应用程序一样,有一些回调函数必须填写且向windows 服务管理器(service manager)进行注册, ...
- C语言编写Windows服务程序
原文:C语言编写Windows服务程序 #include <Windows.h> #include <stdio.h> #define SLEEP_TIME 5000 // 间 ...
随机推荐
- Java 递归调用 recursive 给一个参数 返回一大堆
需求: 需要组装成对象多层嵌套式的 json字符串; 想到使用 递归来完成这个多层嵌套: 憋了四个小时,终于写出来了; 先看效果: 数据库中的数据: 拼装后的效果: [ EmpVO{ ename='孙 ...
- 【ASP.NET MVC 学习笔记】- 15 Unobtrusive Ajax
本文参考:http://www.cnblogs.com/willick/p/3418517.html 1.Unobtrusive Ajax允许我们通过 MVC 的 Help mothod 来定义 Aj ...
- PHP设计模式四:适配器模式
一.什么是适配器模式 适配器模式有两种:类适配器模式和对象适配器模式.其中类适配器模式使用继承方式,而对象适配器模式使用组合方式.由于类适配器 模式包含双重继承,而PHP并不支持双重继承,所以一般都采 ...
- Mybatis报错:Parameter 'list' not found. Available parameters are [groupList, param1]
GroupDao.java 里面定义的方法: void batchInsertLog(@Param("groupList") List<MktPromotionIntegra ...
- 总结:PyQt5自定义信号源
定义一个信号源有4个方面要注意: 1.定义信号源 A = pyqtSignal([str], [int,str]) 这里特别使用信号源重载的情况加以说明.如上就是信号源A的重载,一个可以发送str参数 ...
- <script src="xxx.php"></script>
应热情粉丝的殷切期待,我决定从百忙之中抽出时间来完成这篇博文.(开玩笑啦) 我也是近期才接触到这种引用js的办法.例如,有这样一段js代码 <script src='http://ww.***. ...
- coursera无法观看视频解决方法
coursera无法观看视频解决方法 Coursera是国外的一款非常有名的公开课网站,值得大家一起学习,奈何"长城"太厚,经常被和谐,一些视频打不开,最近找到不用FQ的方法,共享 ...
- Another Eight Puzzle
Problem Description Fill the following 8 circles with digits 1~8,with each number exactly once . Con ...
- 关于php加密库加密数据上传数据库或解密出错的问题
php加密拓展库随着php版本的更新,函数的使用方法有所改变,所以加密模式推荐使用ecb,其中加密算法19种,加密模式8种,通过这种方式加密后的数据上传数据库后提取出来进行解密会发现结果是乱码,我认为 ...
- js foreach、map函数
语法:forEach和map都支持2个参数:一个是回调函数(item,index,input)和上下文: •forEach:用来遍历数组中的每一项:这个方法执行是没有返回值的,对原来数组也没有影响: ...