HDUOJ Highways (最小生成树)
2016-05-01 13:19
381 查看
Highways
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other)Total Submission(s) : 18 Accepted Submission(s) : 6
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
The first line of input is an integer T, which tells how many test cases followed.<br>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.
[align=left]Output[/align]
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.
[align=left]Sample Input[/align]
1
3
0 990 692
990 0 179
692 179 0
[align=left]Sample Output[/align]
692
#include <stdio.h>
int n,m[505][505],dis[505];
void Prim()
{
int vis[505] = {0};
vis[0] = true;
int k=0,minm = 70000;
for(int i=1; i<n; i++)
{
dis[i] = m[0][i];
if(dis[i] < minm) {minm = dis[i]; k = i;}
}
for(int j=1; j<n; j++)
{
minm = 70000;
vis[k] = 1;
for(int i=0; i<n; i++)
{
if(m[k][i]<dis[i] && !vis[i]) {dis[i] = m[k][i];}
}
for(int i=0; i<n; i++)
{
if(!vis[i] && dis[i]<minm)
{
minm = dis[i];
k = i;
}
}
}
}
int main()
{
int t;
scanf("%d", &t);
while(t--)
{
scanf("%d", &n);
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
scanf("%d", &m[i][j]);
}
int maxm = 0;
Prim();
for(int i=0; i<n; i++)
{
if(dis[i]>maxm) maxm = dis[i];
}
printf("%d\n", maxm);
}
return 0;
}
相关文章推荐
- SICP 换零钱的迭代版本
- HDUOJ1864最大报销额(01背包)
- java.lang.Class.forName(String name, boolean initialize, ClassLoader loader)方法
- html中音频和视频
- Android M 6.0 Build about 64-bit (__arm64__)
- SICP 关于递归迭代的重新理解以及尾递归的引入...
- 博客汇总
- 第十一周项目3.1 警察和厨师
- hdoj2036多边形面积的求法
- iOS学习笔记48-Swift(八)反射
- 使用CXF处理JavaBean式的复合类型和List集合类型的形参和返回值
- finsh初步
- 关于网站前后端联调得到的
- jQuery实现点击全选和取消全选
- hdoj2602
- 汇编与驱动-采用SSDT Hook NtOpenProcess保护进程
- iOS学习笔记48-Swift(八)反射
- SICP 1.29-1.33
- SVM学习总结
- 重叠社区发现-LFM算法