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.
Flatopian 城镇从 1 到 N 编号。每条高速公路正好连接两个城镇。所有高速公路都沿直线行驶。所有高速公路都可以双向使用。高速公路可以自由交叉,但司机只能在位于两条高速公路尽头的城镇之间的高速公路之间切换。
弗拉托邦政府希望尽量缩短将要建造的最长高速公路的长度。但是,他们希望保证每个城镇都可以通过高速公路从其他城镇到达。
输入
每个案例的第一行是一个整数 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
暗示
题意描述:给你村庄间的要修公路的距离,求所有的村庄都可以相连的最短路里面的最大的一条路长度
解题思路:先把村庄距离存入相应的二维数组里,利用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; }