博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj 2485 -- Highways
阅读量:6267 次
发布时间:2019-06-22

本文共 3044 字,大约阅读时间需要 10 分钟。

Highways
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 21324   Accepted: 9828

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

130 990 692990 0 179692 179 0

Sample Output

692 水题一道。最小生成树,不解释。
1 /*====================================================================== 2  *           Author :   kevin 3  *         Filename :   Highways.cpp 4  *       Creat time :   2014-07-09 15:05 5  *      Description : 6 ========================================================================*/ 7 #include 
8 #include
9 #include
10 #include
11 #include
12 #include
13 #define clr(a,b) memset(a,b,sizeof(a))14 #define M 60015 #define INF 0x7f7f7f7f16 using namespace std;17 int c[M][M],dis[M];18 int prim(int n)19 {20 bool vis[M];21 int i,j,k,sum = 0;22 for(i = 0; i < n; i++){23 dis[i] = c[0][i];24 vis[i] = false;25 }26 vis[0] = true;27 for(i = 1; i < n; i++){28 int _min = INF;29 j = 0;30 for(k = 0; k < n; k++){31 if(_min > dis[k] && !vis[k]){32 _min = dis[k];33 j = k;34 }35 }36 vis[j] = true;37 if(sum < _min){38 sum = _min;39 }40 for(k = 0; k < n; k++){41 if(dis[k] > c[j][k] && !vis[k]){42 dis[k] = c[j][k];43 }44 }45 }46 return sum;47 }48 int main(int argc,char *argv[])49 {50 int cas,n,value;51 scanf("%d",&cas);52 while(cas--){53 clr(c,0);54 scanf("%d",&n);55 for(int i = 0; i < n; i++){56 for(int j = 0; j < n; j++){57 scanf("%d",&value);58 c[i][j] = value;59 }60 }61 int ans = prim(n);62 printf("%d\n",ans);63 }64 return 0;65 }
View Code

 

转载于:https://www.cnblogs.com/ubuntu-kevin/p/3833709.html

你可能感兴趣的文章
Spring Boot 2.0(二):Spring Boot 2.0尝鲜-动态 Banner
查看>>
Delphi IdTCPClient IdTCPServer 点对点传送文件
查看>>
Delphi中使用ActiveX的一些心得
查看>>
QT5.8.0+MSVC2015安装以及环境配置(不需要安装VS2015)
查看>>
(原創) C/C++的function prototype和header file (C/C++) (C)
查看>>
深入理解JavaScript系列(29):设计模式之装饰者模式
查看>>
程序员的罪与罚
查看>>
SQL*LOADER错误总结
查看>>
SQL日志收缩
查看>>
【转】MySQL Query Cache 小结
查看>>
SVN分支和合并的简单例子
查看>>
PHP实现的封装验证码类
查看>>
Augular初探
查看>>
PHPStorm下XDebug配置
查看>>
【LeetCode】55. Jump Game
查看>>
Android应用盈利广告平台的嵌入方法详解
查看>>
Linux(CentOS6.5) 开放端口,配置防火墙
查看>>
Func与Action
查看>>
Android ViewPager 应该及技巧
查看>>
ODI KM二次开发手册
查看>>