POJ - 2485 Highways (Prim算法)

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.

Flatopia 岛国是完全平坦的。不幸的是,Flatopia 没有公共高速公路。所以Flatopia的交通很困难。弗拉托邦政府意识到了这个问题。他们计划修建一些高速公路,这样就可以在不离开高速公路系统的情况下在任何两个城镇之间开车。

Flatopian 城镇从 1 到 N 编号。每条高速公路正好连接两个城镇。所有高速公路都沿直线行驶。所有高速公路都可以双向使用。高速公路可以自由交叉,但司机只能在位于两条高速公路尽头的城镇之间的高速公路之间切换。

弗拉托邦政府希望尽量缩短将要建造的最长高速公路的长度。但是,他们希望保证每个城镇都可以通过高速公路从其他城镇到达。

输入

输入的第一行是一个整数 T,它告诉我们接下来有多少个测试用例。
每个案例的第一行是一个整数 N (3 <= N <= 500),这是村庄的数量。然后是N行,其中第i个包含N个整数,这N个整数中的第j个就是i村和j村之间的距离(距离应该是[1, 65536]内的整数)。每个测试用例后面都有一个空行。

输出

对于每个测试用例,您应该输出一行包含一个整数,它是连接所有村庄的最长道路的长度,并且该值最小。

Sample Input

1

3
0 990 692
990 0 179
692 179 0

Sample Output

692

暗示

大量输入,推荐使用scanf。

题意描述:给你村庄间的要修公路的距离,求所有的村庄都可以相连的最短路里面的最大的一条路长度

解题思路:先把村庄距离存入相应的二维数组里,利用prim求最短的路径,因为我们在prim里会记录到每条我们的路长度,最后写一个for循环,然后再求所有记录的长度中最长的那一条路。

AC

#include<stdio.h>
#include<string.h>
int main(void)
{
	int n,u,a,v,min,t,inf=9999999;
	int e[550][550],dis[550],book[550];
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d",&n);
		for(int i=1;i<=n;i++)	
			for(int j=1;j<=n;j++)
			if(i==j) e[i][j]=0;
			else e[i][j]=inf;
		for(int i=1;i<=n;i++)
		{	
			for(int j=1;j<=n;j++)
			{
				scanf("%d",&e[i][j]);
			}
		}
		for(int i=1;i<=n;i++)
		{
			dis[i]=e[1][i];
			book[i]=0;
		}
		book[1]=1;
		for(int i=1;i<n;i++)
		{
			min=inf;
			for(int j=1;j<=n;j++)
			{
				if(book[j]==0&&dis[j]<min)
				{
					min=dis[j];
					u=j;
				}
			}
			book[u]=1;
			for(v=1;v<=n;v++)
			{
				if(book[v]==0&&dis[v]>e[u][v])
					dis[v]=e[u][v];
			}	
		}
		int ans=-1;
		for(int i=1;i<=n;i++)
		{
			if(ans<dis[i])
				ans=dis[i];
		//	printf("%d",dis[i]);
		}
		printf("%d\n",ans);
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值